2013-04-05 50 views
0

看起來我無法播放存儲在我的內部應用程序目錄中的視頻。無法從我的內部應用程序目錄播放視頻

我存儲在/data/data/my.package.org/files/

視頻我試圖從那裏使用

String fpath = "/data/data/my.package.org/files/video.mpg" 
Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(Uri.parse(fpath), "video/*"); 

但同時在Android默認的視頻播放器和一些外部錄像機(MX播放器)說「這播放文件然而,當我將視頻保存到SD卡,他們就播放罰款視頻無法播放」。

這是爲什麼?

+0

是你的視頻文件設置爲MODE_WORLD_READABLE? – FoamyGuy 2013-04-05 14:03:45

+0

@FoamyGuy我該怎麼做? – 2013-04-05 14:04:15

+0

將您的視頻文件寫入'files /'目錄的代碼 – FoamyGuy 2013-04-05 14:04:41

回答

0

把你的視頻資產的文件夾,並使用此代碼的MediaPlayer播放視頻

InputStream is = getResources().getAssets().open("video.mpg"); 

OR

把你的視頻資產的文件夾和...

String fpath = "/data/data/my.package.org/assets/video.mpg" 
Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(Uri.parse(fpath), "video/*"); 
+1

我不能把我的視頻資源我在我的應用程序中創建它 – 2013-04-05 14:15:06

0

把你視頻在res \ raw文件夾中。然後將其複製到應用程序(如視頻播放器)可以讀取的外部目錄中。只有您的應用程序可以讀取/數據/數據/等等....

如果在res \原料,然後

  typeName = sourceSink.Types.video.toString(); 
     for (sourceSink.VideoFiles video: sourceSink.VideoFiles.values()){ 
      resourceName = video.toString(); 
      fileName = resourceName + ".mp4"; 
      resource = getResources().getIdentifier(resourceName, "raw", "com.invodo.allshareplay"); 
      createExternalStoragePublicFile(typeName,fileName,resource); 
     } 

,然後你可以用它來複制:

 void createExternalStoragePublicFile(String fType, String fname, int res) { 
     // Create a path where we will place our picture in the user's 
     // public pictures directory. Note that you should be careful about 
     // what you place here, since the user often manages these files. For 
     // pictures and other media owned by the application, consider 
     // Context.getExternalMediaDir(). 
     File path = null; 
     if (((fType.equals(sourceSink.Types.photo.toString())) || (fType.equals(sourceSink.Types.file.toString())))){ 
      path = Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES); 
      } 
     if (fType.equals(sourceSink.Types.music.toString())) { 
      path = Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_MUSIC); 
      } 
     if (fType.equals(sourceSink.Types.video.toString())) { 
      path = Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_MOVIES); 
     } 
     File file = new File(path, "/" + fname); 



     try { 
      // Make sure the Pictures directory exists. 
      path.mkdirs(); 

      // Very simple code to copy a picture from the application's 
      // resource into the external file. Note that this code does 
      // no error checking, and assumes the picture is small (does not 
      // try to copy it in chunks). Note that if external storage is 
      // not currently mounted this will silently fail. 
      InputStream is = getResources().openRawResource(res); 
      OutputStream os = new FileOutputStream(file); 
      byte[] data = new byte[is.available()]; 
      is.read(data); 
      os.write(data); 
      is.close(); 
      os.close(); 

      scanMedia(file); 

     } catch (IOException e) { 
      // Unable to create file, likely because external storage is 
      // not currently mounted. 
      Log.w("ExternalStorage", "Error writing " + file, e); 
     } 
    } 
+0

我已經說過我在飛行中製作我的視頻 – 2013-04-05 19:53:19

0

那麼很簡單 - 只要做出我提供的答案的後半部分並寫出來。/data/data /文件夾永遠不會被除你的應用以外的任何東西所查看。