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 6927335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:02:27+00:00 2026-05-27T11:02:27+00:00

I have seen this question: android how to download an 1mb image file and

  • 0

I have seen this question: android how to download an 1mb image file and set to ImageView
It does not solve my problem as it only shows how to display the bitmap after you already have it.

I am trying to download an image from a URL to have it be displayed with an ImageView on an Android device. I am not sure how to do this.

I’ve looked around a bit on the internet, this is the code I have so far:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //Set local image
    ImageView image = (ImageView) findViewById(R.id.test_image);
    image.setImageResource(R.drawable.test2);

    //Prepare to download image
    URL url;        
    InputStream in;

    //BufferedInputStream buf;
    try {
        url = new URL("http://i.imgur.com/CQzlM.jpg");
        in = url.openStream();

        out = new BufferedOutputStream(new FileOutputStream("testImage.jpg"));
        int i;

         while ((i = in.read()) != -1) {
             out.write(i);
         }
         out.close();
         in.close();

        buf = new BufferedInputStream(in);
        Bitmap bMap = BitmapFactory.decodeStream(buf);
        image.setImageBitmap(bMap);
        if (in != null) {
        in.close();
        }
        if (buf != null) {
        buf.close();
        }
    } catch (Exception e) {
        Log.e("Error reading file", e.toString());
    }
}
  • 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-27T11:02:28+00:00Added an answer on May 27, 2026 at 11:02 am

    The code would probably work, alltough you are downloading your image on your main thread. This means that when it takes more than 5 seconds to download, you will be presented with the famous ANR dialog, and your app will crash…

    You should download your image in a background thread, and post the result back to your main thread. Back in the main thread, you can update your imageview with the downloaded image.

    Here’s an example:

    package nl.entreco.stackoverflow;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.net.URL;
    
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.drawable.BitmapDrawable;
    import android.graphics.drawable.Drawable;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.ImageView;
    
    public class StackOverflowActivity extends Activity {
    
    //  
    private ImageView mImageView;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        //Find the reference to the ImageView
        mImageView = (ImageView) findViewById(R.id.test_image);
    
        // You can set a temporary background here
        //image.setImageResource(null);
    
        // Start the DownloadImage task with the given url
        new DownloadImage().execute("http://i.imgur.com/CQzlM.jpg");
    }
    
    
    /**
     * Simple functin to set a Drawable to the image View
     * @param drawable
     */
    private void setImage(Drawable drawable)
    {
        mImageView.setBackgroundDrawable(drawable);
    }
    
    public class DownloadImage extends AsyncTask<String, Integer, Drawable> {
    
        @Override
        protected Drawable doInBackground(String... arg0) {
            // This is done in a background thread
            return downloadImage(arg0[0]);
        }
    
        /**
         * Called after the image has been downloaded
         * -> this calls a function on the main thread again
         */
        protected void onPostExecute(Drawable image)
        {
            setImage(image);
        }
    
    
        /**
         * Actually download the Image from the _url
         * @param _url
         * @return
         */
        private Drawable downloadImage(String _url)
        {
            //Prepare to download image
            URL url;        
            BufferedOutputStream out;
            InputStream in;
            BufferedInputStream buf;
    
            //BufferedInputStream buf;
            try {
                url = new URL(_url);
                in = url.openStream();
    
                /*
                 * THIS IS NOT NEEDED
                 * 
                 * YOU TRY TO CREATE AN ACTUAL IMAGE HERE, BY WRITING
                 * TO A NEW FILE
                 * YOU ONLY NEED TO READ THE INPUTSTREAM 
                 * AND CONVERT THAT TO A BITMAP
                out = new BufferedOutputStream(new FileOutputStream("testImage.jpg"));
                int i;
    
                 while ((i = in.read()) != -1) {
                     out.write(i);
                 }
                 out.close();
                 in.close();
                 */
    
                // Read the inputstream 
                buf = new BufferedInputStream(in);
    
                // Convert the BufferedInputStream to a Bitmap
                Bitmap bMap = BitmapFactory.decodeStream(buf);
                if (in != null) {
                    in.close();
                }
                if (buf != null) {
                    buf.close();
                }
    
                return new BitmapDrawable(bMap);
    
            } catch (Exception e) {
                Log.e("Error reading file", e.toString());
            }
    
            return null;
        }
    
    }
    
    }
    

    Don’t forget to add the permission.INTERNET to your manifest, if not, you will get an error…

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

Sidebar

Related Questions

I have seen many answers for this type of question but its not related
I have seen this question , but it's not what I'm looking for. There
[Yes I have seen this question but I do not know C nor C++,
I have seen this question and its answers and they clear up some of
I have seen this question that talks about getting the last part of a
I have seen this question about deploying to WebSphere using the WAS ant tasks.
In Microsoft SQL Server: I have seen this question several times, but can't see
Recently I have seen this code in a WebSite, and my question is the
This question may seem a little bit stackoverflow-implementation specific, but I have seen a
I have seen there is no question that specifically reply to this combination of

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.