2015-10-07 69 views
0

我目前有一個應用程序,可以從Android庫導入視頻,製作它們的副本,然後刪除舊文件,如果用戶想要。導出內部存儲的視頻到Android畫廊

現在,我需要用戶能夠反轉該操作並將視頻從應用程序的內部存儲傳輸到Android圖庫。

這裏是我試過到目前爲止:

 //storage/emulated/0/Pictures -- getPath() of line below 
     File androidVidGalleryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 

     File hiddenVideo = new File("/data/data/mypackagename/app_vidDir/vid_10.mp4"); 
     File exportedVid = new File(androidVidGalleryPath.getPath()+"/DesiredAlbumName/"+"DesiredVideoName"); 

     try { 
      FileOutputStream newFile = new FileOutputStream (exportedVid); 
      FileInputStream oldFile = new FileInputStream (hiddenVideo); 

      // Transfer bytes from in to out 
      byte[] buf = new byte[1024]; //this will only read 1KB of data, definitely not enough for an entire video. 
      oldFile.read(buf); 
      newFile.write(buf, 0, buf.length); 
      newFile.flush(); 
      newFile.close(); 
      oldFile.close(); 
     } catch (IOException e) { 
      Toast.makeText(getActivity(),"ERROR",Toast.LENGTH_LONG).show(); 
      e.printStackTrace(); 
     } 

     Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
     File f = new File(exportedVid.getPath()); 
     Uri contentUri = Uri.fromFile(f); 
     mediaScanIntent.setData(contentUri); 
     getActivity().sendBroadcast(mediaScanIntent); 

出於某種原因,沒有任何反應,比嘗試捕捉導致一個IOException警告敬酒等。

我不確定發生了什麼,有什麼想法?

回答

1

你並不需要一個while循環,當你寫的文件:

byte[] buf = new byte[1024]; //this will only read 1KB of data, definitely not enough for an entire video. 
oldFile.read(buf); 
newFile.write(buf, 0, buf.length); 

後您傳輸視頻,做到這一點(從這裏:https://developer.android.com/training/camera/photobasics.html#TaskGallery

private void galleryAddPic() { 
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
    File f = new File(mCurrentPhotoPath); 
    Uri contentUri = Uri.fromFile(f); 
    mediaScanIntent.setData(contentUri); 
    this.sendBroadcast(mediaScanIntent); 
} 
+0

我已經補充說,這個問題仍未解決。看起來好像該文件從未真正被讀取過。我爲此使用了2個示例,其中一個文件爲10+分鐘,另外20個秒。當以類似的方式保存10分鐘的文件時,如果嘗試導出文件需要不到一秒鐘,則需要花費高達45秒的時間。讓我相信這個文件永遠不會被真正閱讀。 –

+0

你有WRITE_EXTERNAL_STORAGE權限嗎? – geokavel

+0

爲什麼不使用File.renameTo()和(檢查返回值以確保它通過)http://developer.android.com/reference/java/io/File.html#renameTo(java.io .File) – geokavel