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

6

我有一个视频文件(.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+"");
     }

感谢任何帮助!


视频的源文件在哪里?它是托管在 HTTP 服务器上吗? - momo
1
https://dev59.com/43A65IYBdhLWcg3wrQl4 可能是重复问题。 - Lalit Poptani
1
看一下这个问题的答案:https://dev59.com/43A65IYBdhLWcg3wrQl4 - bilash.saha
3个回答

13

尝试这个解决方案

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, 0, len1);
            }
            f.close();
    } catch (IOException e) {
        Log.d("Error....", e.toString());
    }

}

1
setDoOutput(true); 在冰激凌三明治中不起作用。 - Zar E Ahmer

0

首先从数据库获取URL,然后将其解析为URI,并粘贴视频URI到DownloadManger构造函数中。 我正在使用onComplete列表来检索来自Firebase数据库的URL。

 viewHolder.ll_download.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                final String vdoUri = task.getResult().toString();

                                try {
                                    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(vdoUri));
                                    request.setDescription("download");
                                    request.setTitle(itemlist.get(position).getFile_name());

                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                                        request.allowScanningByMediaScanner();
                                        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                                    }
                                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, ""+itemlist.get(position).getFile_name()+".mp4");
                                    DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
                                    manager.enqueue(request);
                                }

                                catch (Exception ex) {

                                }


                            }
                        });

0
                URL url = new URL(mp4url);


                //Open a connection to that URL.
                HttpsURLConnection  con = (HttpsURLConnection) 

                 int response =con.getResponseCode();


                InputStream is = con.getInputStream();
                BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
                FileOutputStream outStream = new FileOutputStream(new File(Environment.getExternalStorageDirectory() + "/" + "mnt/sdcard/17.mp4"));
                byte[] buff = new byte[7 * 1024];

                 int len;
                while ((len = inStream.read(buff)) != -1)
                {
                    outStream.write(buff,0,len);
                }
                //clean up
                outStream.flush();
                outStream.close();
                inStream.close();

我在经过很多努力尝试后,终于找到了这个解决方案……它运行良好。 - palasha dbzgt
3和4行怎么样? - V.March

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接