如何在android中从Arraylist下载所有文件?
我想下载所有视频并保存在SD卡中。我在所有文件中都有arraylist。没有一个文件。如何实现它。请帮助我如何在android中从Arraylist下载所有文件?,android,arraylist,download,Android,Arraylist,Download,我想下载所有视频并保存在SD卡中。我在所有文件中都有arraylist。没有一个文件。如何实现它。请帮助我 ArrayList<Url_Dto> list = new ArrayList<Url_Dto>(); 可以在下面下载课程:: class DownloadFileFromURL extends AsyncTask<Object, String, String> { int count = 0; ProgressDia
ArrayList<Url_Dto> list = new ArrayList<Url_Dto>();
可以在下面下载课程::
class DownloadFileFromURL extends AsyncTask<Object, String, String> {
int count = 0;
ProgressDialog dialog;
ProgressBar progressBar;
int myProgress;
/**
* Before starting background thread Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
ProgressBar progressBar;
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(Object... params) {
Log.v("log_tag", "params :::; " + params);
int count;
progressBar = (ProgressBar) params[0];
try {
// URL url = new URL(f_url[0]);
URL url = new URL((String) params[1]);
Log.v("log_tag", "name ::: " + url);
name = ((String) params[1]).substring(((String) params[1])
.lastIndexOf("/") + 1);
Log.v("log_tag", "name Substring ::: " + name);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(),
8192);
download = new File(Environment.getExternalStorageDirectory()
+ "/download/");
if (!download.exists()) {
download.mkdir();
}
String strDownloaDuRL = download + "/" + name;
Log.v("log_tag", " down url " + strDownloaDuRL);
FileOutputStream output = new FileOutputStream(strDownloaDuRL);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
// publishProgress("" + (int) ((total * 100) /
// lenghtOfFile));
// writing data to file
progressBar
.setProgress((int) ((total * 100) / lenghtOfFile));
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
Log.v("log_tag", "progress :: " + values);
// setting progress percentage
// pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
Log.v("log", "login ::: 4::: " + download);
String videoPath = download + "/" + name;
String chpName = name;
Log.v("log_tag", "chpName ::::" + chpName + " videoPath "
+ videoPath);
db.execSQL("insert into videoStatus (chapterNo,videoPath) values(\""
+ chpName + "\",\"" + videoPath + "\" )");
}
}
class DownloadFileFromURL扩展异步任务{
整数计数=0;
进程对话;
ProgressBar ProgressBar;
int-myProgress;
/**
*启动后台线程前显示进度条对话框
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
ProgressBar ProgressBar;
}
/**
*在后台线程中下载文件
* */
@凌驾
受保护的字符串doInBackground(对象…参数){
Log.v(“Log_标记”,“参数::;”+params);
整数计数;
progressBar=(progressBar)参数[0];
试一试{
//URL=新URL(f_URL[0]);
URL URL=新URL((字符串)参数[1]);
Log.v(“日志标签”,“名称::”+url);
名称=((字符串)参数[1])。子字符串(((字符串)参数[1])
.lastIndexOf(“/”+1);
Log.v(“Log_标记”,“名称子字符串::”+名称);
URLConnection conconnection=url.openConnection();
conconnect.connect();
//获取文件长度
int lenghtOfFile=conconnect.getContentLength();
//读取文件的输入流-带8k缓冲区
InputStream输入=新的BufferedInputStream(url.openStream(),
8192);
下载=新文件(Environment.getExternalStorageDirectory()
+“/下载/”;
如果(!download.exists()){
下载.mkdir();
}
字符串strDownloaDuRL=下载+“/”+名称;
Log.v(“Log_标签”,“down url”+strDownloaDuRL);
FileOutputStream输出=新的FileOutputStream(strDownloaDuRL);
字节数据[]=新字节[1024];
长总计=0;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
//发布进度。。。。
//在此之后,将调用onProgressUpdate
//出版进度(“+(整数)((总计*100)/
//长度(笔画);
//将数据写入文件
进度列
.setProgress((int)((总计*100)/长度文件));
输出.写入(数据,0,计数);
}
//冲洗输出
output.flush();
//合流
output.close();
input.close();
}捕获(例外e){
Log.e(“错误:,e.getMessage());
}
返回null;
}
/**
*更新进度条
* */
受保护的void onProgressUpdate(字符串…值){
super.onProgressUpdate(值);
Log.v(“日志标签”,“进度::”+值);
//设置进度百分比
//pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
*完成后台任务后,关闭“进度”对话框
* **/
受保护的void onPostExecute(字符串文件\u url){
Log.v(“日志”,“登录:::4:::”+下载);
字符串videoPath=下载+“/”+名称;
字符串chpName=name;
Log.v(“日志标签”,“chpName::”+chpName+“视频路径”
+视频路径);
db.execSQL(“插入到videoStatus(chapterNo,videoPath)值(\”)
+chpName+“\”,“\”+视频路径+“\”);
}
}
提供了有关http文件下载的想法。有了这个想法,您就可以遍历列表中的所有视频URL。只需使用arraylist大小的for循环,并将下载任务放在这个for循环中。。。
class DownloadFileFromURL extends AsyncTask<Object, String, String> {
int count = 0;
ProgressDialog dialog;
ProgressBar progressBar;
int myProgress;
/**
* Before starting background thread Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
ProgressBar progressBar;
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(Object... params) {
Log.v("log_tag", "params :::; " + params);
int count;
progressBar = (ProgressBar) params[0];
try {
// URL url = new URL(f_url[0]);
URL url = new URL((String) params[1]);
Log.v("log_tag", "name ::: " + url);
name = ((String) params[1]).substring(((String) params[1])
.lastIndexOf("/") + 1);
Log.v("log_tag", "name Substring ::: " + name);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(),
8192);
download = new File(Environment.getExternalStorageDirectory()
+ "/download/");
if (!download.exists()) {
download.mkdir();
}
String strDownloaDuRL = download + "/" + name;
Log.v("log_tag", " down url " + strDownloaDuRL);
FileOutputStream output = new FileOutputStream(strDownloaDuRL);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
// publishProgress("" + (int) ((total * 100) /
// lenghtOfFile));
// writing data to file
progressBar
.setProgress((int) ((total * 100) / lenghtOfFile));
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
Log.v("log_tag", "progress :: " + values);
// setting progress percentage
// pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
Log.v("log", "login ::: 4::: " + download);
String videoPath = download + "/" + name;
String chpName = name;
Log.v("log_tag", "chpName ::::" + chpName + " videoPath "
+ videoPath);
db.execSQL("insert into videoStatus (chapterNo,videoPath) values(\""
+ chpName + "\",\"" + videoPath + "\" )");
}
}