如何下载视频文件到SD卡? [英] How can I download a video file to SD card?

查看:148
本文介绍了如何下载视频文件到SD卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视频文件(.MP4格式),我希望让用户能够将视频下载到他们的SD card.I当前还在使用该code,但它不工作。

I have a video file(.MP4 format) and I want to allow the user to be able to download the video to their SD card.I currently use this code but its not working..

String PATHSdcard = "/sdcard/Video/";  

 public void DownloadFromUrl(String VideoURL, String fileName)         


try { URL url = new URL("https://javmed-prod.s3.amazonaws.com/666034cbe81045f2a2da50e5205e376b.mp4");
             File file = new File(fileName);

             long sTime = System.currentTimeMillis();
             URLConnection URLcon = url.openConnection();

             InputStream is = URLcon.getInputStream();
             BufferedInputStream bis = new BufferedInputStream(is);

             ByteArrayBuffer baf = new ByteArrayBuffer(50);
             int current = 0;
             while ((current = bis.read()) != -1) {
                     baf.append((byte) current);
             }

             FileOutputStream fos = new FileOutputStream(PATHSdcard+file);
             fos.write(baf.toByteArray());
             fos.close();

     } catch (IOException e) {
             Log.d("ERROR.......",e+"");
     }

感谢您​​的帮助!

推荐答案

尝试这种解决方案

包com.Video.ALLTestProject;

package com.Video.ALLTestProject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;


public class VideoSaveSDCARD extends Activity{

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ProgressBack PB = new ProgressBack();
        PB.execute("");
    }

    class ProgressBack extends AsyncTask<String,String,String> {
         ProgressDialog PD;
        @Override
        protected void onPreExecute() {
            PD= ProgressDialog.show(LoginPage.this,null, "Please Wait ...", true);
            PD.setCancelable(true);
        }

        @Override
        protected void doInBackground(String... arg0) {
        DownloadFile("http://beta-vidizmo.com/hilton.mp4","Sample.mp4");            

        }
        protected void onPostExecute(Boolean result) {
            PD.dismiss();

        }

    }



    public void DownloadFile(String fileURL, String fileName) {
        try {
            String RootDir = Environment.getExternalStorageDirectory()
                    + File.separator + "Video";
            File RootFile = new File(RootDir);
            RootFile.mkdir();
            // File root = Environment.getExternalStorageDirectory();
            URL u = new URL(fileURL);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();
            FileOutputStream f = new FileOutputStream(new File(RootFile,
                    fileName));
            InputStream in = c.getInputStream();
            byte[] buffer = new byte[1024];
            int len1 = 0;

            while ((len1 = in.read(buffer)) > 0) {                          
                f.write(buffer, 0, len1);               
            }       
            f.close();


        } catch (Exception e) {

            Log.d("Error....", e.toString());`enter code here`
        }

    }


}

这篇关于如何下载视频文件到SD卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆