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警告敬酒等。
我不確定發生了什麼,有什麼想法?
我已經補充說,這個問題仍未解決。看起來好像該文件從未真正被讀取過。我爲此使用了2個示例,其中一個文件爲10+分鐘,另外20個秒。當以類似的方式保存10分鐘的文件時,如果嘗試導出文件需要不到一秒鐘,則需要花費高達45秒的時間。讓我相信這個文件永遠不會被真正閱讀。 –
你有WRITE_EXTERNAL_STORAGE權限嗎? – geokavel
爲什麼不使用File.renameTo()和(檢查返回值以確保它通過)http://developer.android.com/reference/java/io/File.html#renameTo(java.io .File) – geokavel