Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9065217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:26:49+00:00 2026-06-16T16:26:49+00:00

I have a progress bar managed by a AsyncTask which downloads some files from

  • 0

I have a progress bar managed by a AsyncTask which downloads some files from the internet.

private class DownloadImgs extends AsyncTask<Void, String, Void> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                showDialog(progress_bar_type);
            }


            @Override
            protected Void doInBackground(Void ...params) {
               for(GetCountry gc : listCountry){
           getdownloadedFiles(gc.getUrl());}

           return null;

            }


            protected void onProgressUpdate(String... progress) {

                pDialog.setProgress(Integer.parseInt(progress[0]));
           }

            @Override
            protected void onPostExecute(Void result) {

                dismissDialog(progress_bar_type);


            }

The method which allows me to download the files:

public void getdownloadedFiles(String url)

            {

                int count;
                 try {

                  URL urlImg = new URL(url);
                  URLConnection connection = urlImg.openConnection();
                  connection.connect();

                  int lenghtOfFile = connection.getContentLength();
                  InputStream input = new BufferedInputStream(urlImg.openStream(), 8192);


                  OutputStream output = new FileOutputStream(folder+"/"+name);

                  byte data[] = new byte[1024];

                  long total = 0;

                  while ((count = input.read(data)) != -1) {
                      total += count;

                     publishProgress(""+(int)((total*100)/lenghtOfFile));

                      output.write(data, 0, count);

                  }


                  output.flush();           
                  output.close();
                  input.close();

                 } catch (Exception e) {
                     e.getMessage();
                 }


            }

This code works well, but the problem is that each file downloaded has it’s own progress bar
I want only one progress bar for all the downloaded files.

How can I achieve this?
Thank you very much

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:26:50+00:00Added an answer on June 16, 2026 at 4:26 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a progress bar which inherits from JSlider to provide highlighting functionality. Highlights
I have managed to use an Asynctask with an indeterminate progress bar during screen
I have this progress bar that uses callback functions from a third party driver,
i am trying to have a horizontal progress bar, for which, it starts with
I have seen progress bar in mobile which are working continuously while processing data
I have a progress bar which when it hits 100%, and if given a
I have a progress bar which I create in code my activity. This is
I have created a C# COM DLL which is essentially an enhanced progress bar
I have a progress bar to show the status of the program loading songs
i have used progress bar in my application..my application is working fine but the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.