views:

305

answers:

3

What is the simplest approach to the following in Android:

  1. Load an image from a remote server.
  2. Display it in an ImageView.

In other words, if you had to explain to someone how easy it is to work with remote images in Android how would you do it?

A: 

The easiest way so far is build a simple image retriver:

public Bitmap getRemoteImage(final URL aURL) {
    try {
        final URLConnection conn = aURL.openConnection();
        conn.connect();
        final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
        final Bitmap bm = BitmapFactory.decodeStream(bis);
        bis.close();
        return bm;
    } catch (IOException e) {}
    return null;
}

Then, you just have to supply a URL to the method and it will returns a Bitmap. Then, you will just have to use the setImageBitmap method from ImageView to show the image.

Cristian
+3  A: 

See ImageView.setImageURI. Something like:

myImageView.setImageURI(Uri.parse("http://example.com/logo.png"));

And you're done.


Edit: I have not tested the above method and, from the comments, it seems it doesn't work. Here's another method that I actually used in an application and I know it works:

try {
    URL thumb_u = new URL("http://www.example.com/image.jpg");
    Drawable thumb_d = Drawable.createFromStream(thumb_u.openStream(), "src");
    myImageView.setImageDrawable(thumb_d);
}
catch (Exception e) {
    // handle it
}

I have no idea what the second parameter to Drawable.createFromStream is, but passing "src" seems to work. If anyone knows, please shed some light, as the docs don't really say anything about it.

Felix
That's so much better... thanks for the tip!
Cristian
Have you tested this? Doesn't seem to be working here.
Emanuil
No, this doesn't work.
alexanderblom
I'm sorry if this doesn't work, I have not tested it. I have edited my answer and added another method.
Felix
+1  A: 

Be careful with both of the answers here - they both run the chance of an OutOfMemoryException. Test your application by attempting to download a large image, such as a desktop wallpaper. To be clear, the offending lines are :

final Bitmap bm = BitmapFactory.decodeStream(bis);

and

Drawable thumb_d = Drawable.createFromStream(thumb_u.openStream(), "src");

Felix's answer will catch it in the catch{} statement, and you could do something about it there.

Here is how to work around the OutOfMemoryException error:

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 8;
    Bitmap bmp = null;
    try {
        bmp = BitmapFactory.decodeStream(is, null, options);
    } catch (OutOfMemoryError ome) {
        // TODO - return default image or put this in a loop,
        // and continue increasing the inSampleSize until we don't
        // run out of memory
    }

And here are my comments on this in my code

/**
 * Showing a full-resolution preview is a fast-track to an
 * OutOfMemoryException. Therefore, we downsample the preview image. Android
 * docs recommend using a power of 2 to downsample
 * 
 * @see <a
 *      href="http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966"&gt;StackOverflow
 *      post discussing OutOfMemoryException</a>
 * @see <a
 *      href="http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize"&gt;Android
 *      docs explaining BitmapFactory.Options#inSampleSize</a>
 * 
 */

Links from above comments: Link 1 Link 2

Hamy