2
I have an Asynctask that downloads several images that I pass through an Arraylist with the links to download and I use a Progress bar and it is not working perfectly.
The progress bar restarts to each link that downloads the image, I think I have to add size to each image of the links and then start to make the bar Progress to run, to test I have an Arraylist with 9 links the bar Progress restarts 9 times.
what would be and best way to implement a multi-download Progress bar?
class DownloadTask extends AsyncTask<ArrayList<String>, Integer, String> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setCancelable(false);
progressDialog.setTitle("Download em progresso...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(100);
progressDialog.setProgress(0);
progressDialog.show();
}
@Override
protected String doInBackground(ArrayList<String>... params) {
ArrayList<String> path = params[0];
int count = 0;
for (String urlImage : path) {
int file_length = 0;
try {
URL url = new URL(urlImage);
URLConnection urlconnection = url.openConnection();
urlconnection.connect(); // ok
file_length = urlconnection.getContentLength();
String fileName = Uri.parse(String.valueOf(url)).getLastPathSegment();
File imput_file = new File(new_folder, fileName);
InputStream inputStream = new BufferedInputStream(url.openStream(), 8192);
byte[] data = new byte[1024];
int total = 0;
OutputStream outputStream = new FileOutputStream(imput_file);
while ((count = inputStream.read(data)) != -1) {
total += count;
outputStream.write(data, 0, count);
int progress = (int) total * 100 / file_length;
publishProgress(progress);
}
inputStream.close();
outputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return "Download finalizado!";
}
@Override
protected void onProgressUpdate(Integer... values) {
progressDialog.setProgress(values[0]);
}
@Override
protected void onPostExecute(String result) {
progressDialog.hide();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
thanks.
I reversed its edition, which adds [Closed] to the title, because this statement is made when accepting a reply.
– ramaral