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);

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);

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.


adil said...

Credit for the above post goes to:

orang indonesia said...

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