[英]Download multiple files with one progressbar java / Android
我正在for()循環的幫助下在AsyncTask中下載多個文件。 下面的代碼可以正常工作,但是每個下載的文件都有自己的單個進度條,我只希望所有下載文件都有一個進度條。
// ProgressDialog for downloading images
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type:
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setTitle("In progress...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
default:
return null;
}
}
下面是用於下載文件的AsyncTask。
class DownloadFileFromURL extends AsyncTask<String, Integer, String> {
/**
* Before starting background thread Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try {
for (int i = 0; i < f_url.length; i++) {
URL url = new URL(f_url[i]);
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);
System.out.println("Data::" + f_url[i]);
// Output stream to write file
OutputStream output = new FileOutputStream(
"/sdcard/Images/" + i + ".jpg");
byte data[] = new byte[1024];
long total = 0;
int zarab=20;
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
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
//cc++;
}
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(Integer... progress) {
// setting progress percentage
pDialog.setProgress(progress[0]);
}
/**
* After completing background task Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
// Displaying downloaded image into image view
// Reading image path from sdcard
//String imagePath = Environment.getExternalStorageDirectory()
// .toString() + "/downloaded.jpg";
// setting downloaded into image view
// my_image.setImageDrawable(Drawable.createFromPath(imagePath));
}
}
或者,如果進度欄顯示並針對“文件數”進行了升級,則表示該方法不是替代lenghtOfFile,也將是替代且有用的解決方案。 任何幫助將不勝感激。
我認為您有2種選擇:
假進度條方法
您預先知道需要下載多少個文件,可以將ProgressDialog
總數設置為要下載的文件數。 這對於尺寸較小且尺寸相似的文件非常有效,並且可以為用戶提供有關發生情況的良好反饋。
// you can modify the max value of a ProgressDialog, we modify it
// to prevent unnecessary rounding math.
// In the configuration set the max value of the ProgressDialog to an int with
pDialog.setMax(urls.length);
for (int i = 0; i < urls.length; i++) {
// launch HTTP request and save the file
//...
// your code
//...
//advance one step each completed download
publishProgress();
}
/**
* Updating progress bar
*/
protected void onProgressUpdate(Integer... progress) {
pDialog.incrementProgressBy(1);
}
真正的進度條方法
您需要事先知道需要下載的所有文件的總長度。 例如,您可以創建一個單獨的REST API,以便在開始下載每個單獨的文件之前先調用所有其他內容,從而獲得總長度(以字節為單位)。 這樣,您可以根據已下載的總字節數定期更新ProgressDialog
的總長度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.