Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

java - Android : Copy RawFile to Sdcard (video mp4)

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();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...