gpt4 book ai didi

android - 将视频保存到内部存储

转载 作者:行者123 更新时间:2023-11-29 00:07:31 25 4
gpt4 key购买 nike

我有一个 videoView,它从与 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 中的视频私下存储到我的应用程序的内部存储器中。

我已经设法使用

对照片执行此操作
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();
}

我怎样才能为视频做同样的事情?

以及如何从内部存储器中读取视频?

最佳答案

这是将视频私下保存到您应用的内部存储器的代码。以及从内部存储中读取视频的代码。希望这会有所帮助。

//For saving Video...

private void saveVideoToInternalStorage (String filePath) {

File newfile;

try {

File currentFile = new File(filePath);
String fileName = currentFile.getName();

ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("videoDir", Context.MODE_PRIVATE);


newfile = new File(directory, fileName);

if(currentFile.exists()){

InputStream in = new FileInputStream(currentFile);
OutputStream out = new FileOutputStream(newfile);

// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;

while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}

in.close();
out.close();

Log.v("", "Video file saved successfully.");

}else{
Log.v("", "Video saving failed. Source file missing.");
}



} catch (Exception e) {
e.printStackTrace();
}

}

private void loadVideoFromInternalStorage(String filePath){

Uri uri = Uri.parse(Environment.getExternalStorageDirectory()+filePath);
myVideoView.setVideoURI(uri);

}

关于android - 将视频保存到内部存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32793481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com