android – 如何将视频文件下载到SD卡?

我有一个视频文件(.MP4格式),我想让用户能够将视频下载到他们的SD卡.我目前使用此代码,但它无法正常工作..
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+"");
     }

谢谢你的帮助!

解决方法

试试这个解决方
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("");
    }

    private 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();

        }

    }



    private void downloadFile(String fileURL,String fileName) {
        try {
        String rootDir = Environment.getExternalStorageDirectory()
                + File.separator + "Video";
           File rootFile = new File(rootDir);
           rootFile.mkdir();
            URL url = new URL(fileURL);
            HttpURLConnection c = (HttpURLConnection) url.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,len1);
            }
            f.close();
    } catch (IOException e) {
        Log.d("Error....",e.toString());
    }

}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...