Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何保存视频?_Android - Fatal编程技术网

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

如何将视频从我的原始文件夹保存到sd卡android,我使用此代码,但商店屏幕上的视频是黑色的,无法播放

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