Android 下载mp4到File

//初始化
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();
    }
}

//下载视频
public void saveVideoToFile(final List 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();
}
//将视频保存到本地
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) {
    }
}
//获取视频地址
public  File[] getVideoFiles(){
    return videoFile.listFiles();
}

你可能感兴趣的:(Android 下载mp4到File)