创建视频(.mp4)文件android的副本



我想复制一个现有的视频文件到另一个视频文件。

我试着这样做:

    byte c;
    try {
        FileOutputStream newFile = new FileOutputStream (VIDEO_PATH_TMP);
        FileInputStream oldFile = new FileInputStream (VIDEO_PATH);
        while ((c = (byte) oldFile.read()) != -1) {
            newFile.write(c);
        }
        newFile.close();
        oldFile.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

但是它不起作用。输出文件已创建,但无法看到视频。

我如何实现这个?

谢谢!

好的,我找到了答案,这是代码:

 try {
        FileOutputStream newFile = new FileOutputStream (VIDEO_PATH_TMP);
        FileInputStream oldFile = new FileInputStream (VIDEO_PATH);
         // 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.close();
        oldFile.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

我只是添加了缓冲数组,它解决了这个问题:)

我认为你应该在close之前调用flush:

newFile.flush();
newFile.close();

最新更新