Download Files In A Single Progress Bar
Solution 1:
In your doInBackground()
method, simply loop over the list of files you need to download inside of the single execution of your AsyncTask
. You should probably also leverage the varargs nature of execute()
to pass the list of URLs you need to download. In other words, something close to this:
DownloadImgs task = new DownloadImgs();
task.execute(url1, url2, url3, url4, url5);
The modify the task as such:
private class DownloadImgs extends AsyncTask<String, String, Void> {
@Override
protected Void doInBackground(String ...params) {
for (String url : params) {
getdownloadedFiles(url);
}
return null;
}
public void getdownloadedFiles(String url) {
/* Existing code */
}
/* Other methods unchanged */
}
EDIT:
One method you could use to display all the file downloads as a single contiguous progress indicator is simply to update the progress by file count alone rather than file content. In other words, if you have 5 files just publishProgress()
with values of 20 (1 * 100 / 5), 40 (2 * 100 / 5), 60 (3 * 100 / 5), 80 (4 * 100 / 5), and 100 (5 * 100 / 5) at the end of each file download.
If you need something more granular without pre-fetching the content length of every file, make each chunk increment with percentage. Note that you can also set the maximum level of the progress bar with setMax()
to something other than 100 to make the math easier to work with. In the same 5 files example, you can set the progress bar maximum to 500, and for each download you would still add 100 to the total in the same fashion as you are now, but the total does not reset at the beginning of each download.
Solution 2:
Pass multiple URLs to your async task.
new DownloadImgs().execute(URL1,URL2,URL3);
it will show whole progress as a single progress bar.
Post a Comment for "Download Files In A Single Progress Bar"