Convert an Image URL to Bitmap in Android

September 21, 2010

Here is a handy static method to retrieve a thumbnail from a url and return a bitmap that can be used in your Android app e.g. assigned to an ImageView in your ListView or some other Layout.

public static Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
Advertisements

2 Responses to “Convert an Image URL to Bitmap in Android”

  1. schwiz Says:

    sweet, one thing though if you use a HttpClient instead you can be sure to handle any kind of redirects.

  2. Miles Porter Says:

    I ran into several issues with the above approach, and wanted to list them here in the hopes that I might save someone else some time.

    1. When load images from urls, I was using an app running in the arm-emulator, and loading images from my local machine. In order to do this, I needed to remember that you can use 10.0.2.2 in the emulator to access your “localhost”.

    2. When loading the images using the above approach, my bitmaps always had width and height of -1. It too me a while, but I finally figured out that if I used the decodeByteArray rather than decodeStream method, things worked better.

    3. Android does not allow for network calls in the main thread, so you will need to run your calls in a thread or AsyncTask.

    4. Remember that you need to set a permission in your mainfest in order to allow you app to access the web.

    My complete (fairly messy) method code is listed below: (good luck!)

    public Bitmap get_bitmap(String url) {
    url = url.replaceAll(“0.0.0.0”, “10.0.2.2”);
    Bitmap x=null;
    URL myFileUrl =null;
    try {
    myFileUrl= new URL(url);
    } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
    conn.setDoInput(true);
    conn.connect();
    InputStream is = conn.getInputStream();
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    int nRead;
    byte[] data = new byte[16384];

    while ((nRead = is.read(data, 0, data.length)) != -1) {
    buffer.write(data, 0, nRead);
    }

    buffer.flush();

    Bitmap raw_image = BitmapFactory.decodeByteArray(buffer.toByteArray(), 0, buffer.size());
    x = Bitmap.createScaledBitmap(raw_image,75, 75, true);

    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return x;
    }
    }


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: