Android:将RawFile复制到SD卡(视频mp4)

时间:2010-07-30 00:04:39

标签: java android video android-sdcard

此代码有什么问题?
我的项目中有一个Raw文件(mp4 videofile),
当我这样做,然后我从SD卡文件retreive文件不相同,所以视频无法加载:(
你有另一种方法可以自动将原始文件复制到SD卡吗? 感谢

String FICHIER_BLOW = "blowvid4.mp4";
File f=new File(Environment.getExternalStorageDirectory(), FICHIER_BLOW);
try {
    if (f.createNewFile()){
    FileWriter ecrivain = new FileWriter(f);
    BufferedWriter bufEcrivain = new BufferedWriter(ecrivain);
    BufferedInputStream VideoReader = new BufferedInputStream(getResources().openRawResource(R.raw.blow));
    while( VideoReader.available() > 0 ){
        bufEcrivain.write(VideoReader.read());
    }
    bufEcrivain.close();

    VideoView videoView = (VideoView) findViewById(R.id.VideoView);
    MediaController mediaController = new MediaController(this);
    mediaController.setAnchorView(videoView);
    video =Uri.fromFile(f);
    videoView.setMediaController(mediaController);
    videoView.setVideoURI(video);
    videoView.start();
    }
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

3 个答案:

答案 0 :(得分:9)

如果使用InputStream进行读取,请使用OutputStream进行写入,即使用BufferedOutputStream包装的FileOutputStream。此外,您的代码效率很低,因为它一次只复制一个字节。我建议创建一个字节数组缓冲区并使用这些相关的读/写方法:

int BufferedInputStream.read(byte[] buffer, int offset, int length)
void BufferedOutputStream.write(byte[] buffer, int offset, int length)

答案 1 :(得分:7)

它有效,谢谢

BufferedOutputStream bufEcrivain = new BufferedOutputStream((new FileOutputStream(f)));
BufferedInputStream VideoReader = new BufferedInputStream(getResources().openRawResource(R.raw.blow));
byte[] buff = new byte[32 * 1024];
int len;
while( (len = VideoReader.read(buff)) > 0 ){
    bufEcrivain.write(buff,0,len);
}
bufEcrivain.flush();
bufEcrivain.close();

答案 2 :(得分:2)

我认为你应该在关闭流之前刷新

bufEcrivain.flush();
bufEcrivain.close();