Progress Bar with Asynctask and multiple download links

Asked

Viewed 379 times

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.

1 answer

2


To make it work, file_length, in int progress = (int) total * 100 / file_length would have to be the sum of the lengths of all images.
This would require that, before reading the images, you have to create and open a Connection and use urlconnection.getContentLength() for each of them, which would not be very efficient.

One possible approach is to evolve the Progressbar each time an image is downloaded:

@Override
protected String doInBackground(ArrayList<String>... params) {

    ArrayList<String> path = params[0];
    int count = 0;
    int imagesCount = path.size();

    for(int i = 0; i < imagesCount; i++){

        try {
            URL url = new URL(urlImage.get(i));

            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);
            }
            inputStream.close();
            outputStream.close();

            int progress = i * 100 / imagesCount;
            publishProgress(progress);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return "Download finalizado!";
}
  • 1

    That’s just what I needed...!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.