Android download mp4 to File

//initialization
private File videoFile;
private void init(){
    String root = Environment.getExternalStorageDirectory().toString();
    videoFile = new File(root + File.separator + "jbrVideo");
    if (!videoFile.exists()|| !videoFile.isDirectory()){
        videoFile.mkdirs();
    }
}

//download video
public void saveVideoToFile(final List<String> list){
    new Thread(){
        @Override
        public void run() {
            for (int i = 0; i < list.size(); i++) {
                try {
                    URL url = new URL(list.get(i));
                    InputStream is = url.openStream();
                    writeOs(is,i);
                } catch (MalformedURLException e) {
                } catch (IOException e) {
                }
            }
        }
    }.start();
}
//Save the video locally
private void writeOs(InputStream is, int position){
    OutputStream os = null;
    try {
        File file = new File(videoFile.getAbsolutePath(), "video" + position + ".mp4");
        TipUtil.log("file : " + file.toString());
        os = new FileOutputStream(file);
        byte buf[] = new byte[2*1024];
        int read = 0;
        while ((read = is.read(buf)) != -1){
            os.write(buf,0,read);
        }
        os.flush();
        os.close();
        is.close();
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }
}
//Get video address
public  File[] getVideoFiles(){
    return videoFile.listFiles();
}

Guess you like

Origin blog.csdn.net/qq_41873558/article/details/100363898