Android:将RawFile复制到Sdcard(视频mp4)
这段代码有什么问题?
我的项目中有一个原始文件(mp4 视频文件),
当我这样做时,然后我从 SD 卡文件中检索的文件不相同,因此无法加载视频:(
您还有另一种方法可以自动将原始文件复制到 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();
}
What is wrong on this code ?
I've a Raw file in my project (mp4 videofile),
when i do this, and then i retreive file from SDcard file are not identical so video can not be load :(
Do you have another way to automaticly copy a raw file to sdcard ?
Thanks
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果使用InputStream读取,则使用OutputStream写入,即BufferedOutputStream包裹的FileOutputStream。此外,您的代码效率非常低,因为它一次只复制一个字节。我建议创建一个字节数组缓冲区并使用这些相关的读/写方法:
If you use an InputStream to read, use an OutputStream to write, i.e. a BufferedOutputStream-wrapped FileOutputStream. Also, your code is pretty inefficient, as it only copies one byte at a time. I'd suggest creating a byte array buffer and using these relevant read/write methods:
它有效,谢谢
It works,thanks
我认为你应该在关闭流之前冲洗
I think you should flush before you close the stream