将视频保存到内部存储
问题描述:
我已经从对应于视频文件Android图库将视频保存到内部存储
VideoView videoView1 = (VideoView)promptsView.findViewById(R.id.videoView09);
String SrcPath = "/storage/emulated/0/DCIM/Camera/20150824_210148.mp4";
videoView1.setVideoPath(SrcPath);
videoView1.requestFocus();
videoView1.start();
现在内的路径起着视频videoView,我需要以某种方式将视频从这个videoView商店内部存储我的应用程序私人。
我已经成功地为照片做到这一点使用
public String saveImageToInternalStorage(Bitmap image, String imgRequestedName) {
ContextWrapper cw = new ContextWrapper(getActivity());
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
File mypath=new File(directory,imgRequestedName+".jpg");
String loc = mypath.getAbsolutePath();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
image.compress(Bitmap.CompressFormat.JPEG, 70, fos);
SharedPreferences pref = getActivity().getApplicationContext().getSharedPreferences("MyPref", Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = pref.edit();
editor.putInt("totalImageCount",(pref.getInt("totalImageCount",0))+1);
editor.commit();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return mypath.getAbsolutePath();
}
我怎样才能做视频的相同呢?
以及如何从内部存储器读取视频?
答
使用它的路径内部存储保存视频:
ContextWrapper cw = new ContextWrapper(getActivity());
File directory = cw.getDir("vidDir", Context.MODE_PRIVATE);
File mypath=new File(directory,vidRequestedName+".mp4");
try {
FileOutputStream newFile = new FileOutputStream (mypath);
//path 0 = current path of the video
FileInputStream oldFile = new FileInputStream (path0);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = oldFile.read(buf)) > 0) {
newFile.write(buf, 0, len);
}
newFile.flush();
newFile.close();
oldFile.close();
答
您应该使用uri
。在示例中,我在资源中创建了一个“原始”文件,但您可以根据需要进行修改。
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.example.mp4);
videoView.setVideoURI(uri);