Android 如何保存视频?
如何将视频从我的原始文件夹保存到sd卡android,我使用此代码,但商店屏幕上的视频是黑色的,无法播放Android 如何保存视频?,android,Android,如何将视频从我的原始文件夹保存到sd卡android,我使用此代码,但商店屏幕上的视频是黑色的,无法播放 public boolean onTouch(View v, MotionEvent event) { boolean success = false; // make the directory File vidDir = new File(android.os.Environment.getExternalStoragePublicDirectory(Envi
public boolean onTouch(View v, MotionEvent event) {
boolean success = false;
// make the directory
File vidDir = new File(android.os.Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) + File.separator + "MY VIDEO");
vidDir.mkdirs();
// create unique identifier
Random generator = new Random();
int n = 100;
n = generator.nextInt(n);
// create file name
String videoName = "Video_" + n + ".mp4";
File fileVideo = new File(vidDir.getAbsolutePath(), videoName);
try {
fileVideo.createNewFile();
success = true;
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(), "Video saved!",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during video saving", Toast.LENGTH_LONG).show();
}
return true;
}
“我使用这段代码,但商店屏幕上的视频是黑色的”——嗯,除此之外,您还没有向该文件写入任何内容
createNewFile()
只创建一个零字节文件。在Resources
对象上使用openRawResource()
获取原始资源上的InputStream
,在文件上打开FileOutputStream
,并使用Java I/O将字节从InputStream
复制到FileOutputStream
。情况如何?createNewFile()只创建一个零字节文件。在Resources对象上使用openRawResource()获取原始资源上的InputStream,在文件上打开FileOutputStream,并使用Java I/O将字节从InputStream复制到FileOutputStream