将内部存储的视频导出到 android 图库
Export internally stored video to the android gallery
我目前有一个应用程序可以从 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);
出于某种原因,除了 try catch 导致 IOException 的警告吐司外,没有任何反应。
我不确定发生了什么,有什么想法吗?
写入文件时不需要 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);
}
我目前有一个应用程序可以从 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);
出于某种原因,除了 try catch 导致 IOException 的警告吐司外,没有任何反应。
我不确定发生了什么,有什么想法吗?
写入文件时不需要 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);
}