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

  • SEARCH
  • Home
  • 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 6547855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:52:54+00:00 2026-05-25T11:52:54+00:00

I need to download a image in a AsyncTask and display progress in a

  • 0

I need to download a image in a AsyncTask and display progress in a progressDialog, the only thing i cant manage to do here is figure out how to update the progressbar properly with a 1024 bytes step, currently i have this and it doesn’t work

class DownloadImageTask extends AsyncTask<String, Integer, Void> {

    @Override
    protected Void doInBackground(String... url) {
        bitmap = DownloadImage(url[0]);
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... args) {
        mProgressDialog.setProgress(args[0]);
    }

    @Override
    protected void onPostExecute(Void unused) {
        ImageView img = (ImageView) findViewById(R.id.img);
        img.setImageBitmap(bitmap);
        img.setVisibility(View.INVISIBLE);              
        imgInfo = "Height: " + bitmap.getWidth() + " px" + "\n" + "Width: "
                + bitmap.getHeight() + " px" + "\n" + "File Size: "
                + fileSize / 1024 + " KB" + "\n" + "Image Name: "   
                + getString(R.string.img_name);             
        isDownloaded = true;          
        mProgressDialog.dismiss();
    }

    @Override
    protected void onPreExecute() {
        mProgressDialog = new ProgressDialog(AndroidActivity.this);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
    }

    private Bitmap DownloadImage(String URL) {
        InputStream in = null;
        try {
            in = OpenHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return bitmap;
    }

    private InputStream OpenHttpConnection(String urlString)
            throws IOException {
        InputStream in = null;
        int response = -1;
        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();
        try {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();
            fileSize = conn.getContentLength();
            response = httpConn.getResponseCode();
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();

                // loop with step 1kb
                byte data[] = new byte[1024];
                long total = 0;
                int count = 0;
                while ((count = in.read(data)) != -1) {
                    total += count;
                    publishProgress((int)(total*100/fileSize));
                } 

            }  
        } catch (Exception ex) {
            throw new IOException("Error connecting");
        }
        return in;
    }
}

can someone help ?

  • 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-05-25T11:52:55+00:00Added an answer on May 25, 2026 at 11:52 am

    You better return the bitmap from doInBackground and take it from parameter in onPostExecute()

    class DownloadImageTask extends AsyncTask<String, Integer, Bitmap> {
    
    @Override
    protected Bitmap doInBackground(String... url) {
        Bitmap bitmap = DownloadImage(url[0]);
        return bitmap ;
    }
    
    @Override
    protected void onProgressUpdate(Integer... args) {
        mProgressDialog.setProgress(args[0]);
    }
    
    @Override
    protected void onPostExecute(Bitmap bmp) {
        ImageView img = (ImageView) findViewById(R.id.img);
        img.setImageBitmap(bmp);
        img.setVisibility(View.INVISIBLE);              
        imgInfo = "Height: " + bitmap.getWidth() + " px" + "\n" + "Width: "
                + bitmap.getHeight() + " px" + "\n" + "File Size: "
                + fileSize / 1024 + " KB" + "\n" + "Image Name: "   
                + getString(R.string.img_name);             
        isDownloaded = true;          
        mProgressDialog.dismiss();
    }
    

    **

    EDIT :

    **
    Once inputstream reaches its end, you cannot read anymore data from it. You should write the InputStream to a byte array instead and then convert the bytearray to a bitmap.

    private Bitmap OpenHttpConnection(String urlString)
            throws IOException {
    ........
    ........
       byte data[] = new byte[1024];
       long total = 0;
       int count = 0;
       ByteArrayOutputStream bos = new ByteArrayOutputStream();
    
       while ((count = in.read(data)) != -1) {
           total += count;
           bos.write(data, 0, count);
           publishProgress((int)(total*100/fileSize));
       } 
    
       return BitmapFactory.decodeByteArray(bos.toByteArray(),0,bos.size());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to download an image from the web and display it in an
I need to download an image from some URL to my server. However, my
I need to download an image from the internet, in a different thread, and
I need to be able to download an image from a remote web server,
How to download an image from the web and display it in an UIImageView?
Assume captcha key is invalid, it need to download new captcha image again and
I have a list of around 3000 image URLs, where I need to download
When we need to download an image from some URL and show it on
I need a simple script that will download a image file to the user's
I need to download images from a website, and I have the login name

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.