Display Images from the Internet in Android

The Overview

Android ImageVIew
I’ll make my usual yammering quick so you can get straight to the good stuff. This is just a snippet to show you how you can display an image from the Internet in your native Android app via ImageView. This is ideal in a few situations, namely when you need to keep app size and initial load time down, or if it isn’t feasible to locally store all the images your app will use.

The Code

package com.savagelook;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
public class ShowImageActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        // YOUR_LAYOUT is the name of your layout resource
        // IMAGEVIEW_ID is your ImageView in YOUR_LAYOUT
        ImageView imageView = (ImageView)findViewById(R.id.IMAGEVIEW_ID); 
    private Drawable createDrawableFromURL(String urlString) {
        Drawable image = null;
        try {
            URL url = new URL(urlString);
            InputStream is = (InputStream)url.getContent();
            image = Drawable.createFromStream(is, "src");
        } catch (MalformedURLException e) {
            // handle URL exception
            image = null;
        } catch (IOException e) {
            // handle InputStream exception
            image = null;
        return image;

4 Responses to “Display Images from the Internet in Android”

  1. Robin says:

    Great explaining, just what i needed.

  2. Emile says:

    Image still not loading. no error messgaes no nothing. it jus does not show. i even tried . still nothing.

  3. Emile says:

    got it working. it seems like size of the image was a problem

  4. Hamzeh says:

    Thanks, was very helpful.