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

android – 如何将视频文件下载到SD卡?,第1张

概述我有一个视频文件(.MP4格式),我想让用户能够将视频下载到他们的SD卡.我目前使用此代码,但它无法正常工作.. String PATHSdcard = "/sdcard/Video/"; public void DownloadFromUrl(String VideoURL, String fileName) try { URL url = new URL("htt 我有一个视频文件(.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 – 如何将视频文件下载到SD卡?全部内容,希望文章能够帮你解决android – 如何将视频文件下载到SD卡?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1134208.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存