我目前有一个应用程序可以从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。
我不确定发生了什么,有什么想法吗?
答案 0 :(得分: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);
}