What is the simplest approach to the following in Android:
- Load an image from a remote server.
- 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?
What is the simplest approach to the following in Android:
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?
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.
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.
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">StackOverflow
* post discussing OutOfMemoryException</a>
* @see <a
* href="http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize">Android
* docs explaining BitmapFactory.Options#inSampleSize</a>
*
*/