Android:将RawFile复制到Sdcard(视频mp4)
问题内容:
此代码有什么问题?
我在项目中有一个Raw文件(mp4视频文件),
当我这样做时,然后我从SDcard文件获取的文件不相同,因此无法加载视频:(
您是否有另一种自动将原始文件复制到的方法sdcard?
谢谢
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();
}
问题答案:
如果使用InputStream进行读取,请使用OutputStream进行写入,即BufferedOutputStream包装的FileOutputStream。另外,您的代码效率很低,因为一次只能复制一个字节。我建议创建一个字节数组缓冲区并使用以下相关的读/写方法:
int BufferedInputStream.read(byte[] buffer, int offset, int length)
void BufferedOutputStream.write(byte[] buffer, int offset, int length)