Thursday 25 August 2011

Android: set ImageView image content from a url

Turns out the following code doesn't set the image content of an ImageView:

Uri myImageUri = Uri.parse(myImageAddress);
myImageView.setImageURI(imageUri);

If you inspect the LogCat log, you'll see a message something like "resolveUri failed on bad bitmap uri". Here's what you need to do instead...

URL myUrl = new URL(myImageAddress);
InputStream inputStream = (InputStream)myUrl.getContent();
Drawable drawable = Drawable.createFromStream(inputStream, null);
myImageView.setImageDrawable(drawable);

That should work once you've wrapped the necessary try/catch statements around it and set the "internet" uses-permission in the AndroidManifest file of course.

2 comments:

adil said...

Credit for the above post goes to:
http://asantoso.wordpress.com/2008/03/07/download-and-view-image-from-the-web/

orang indonesia said...

thanks, this is work. I can load imageview to my listview