-1

Possible Duplicate:
Loading remote images

How to put a picture from the Internet in a ImageView?

ImageView image = (ImageView) findViewById(R.id.main_layout_image_cover);

image.(?!);

Community
  • 1
  • 1
ruslanys
  • 1,183
  • 2
  • 13
  • 25

2 Answers2

1

Here use this:

URL imageURL = new URL("http://....");
Bitmap image = BitmapFactory.decodeStream(imageUrl.openConnection().getInputStream());
ImageView imageView = (ImageView) findViewById(R.id.main_layout_image_cover);
imageView.setImageBitmap(image);

Of course this will block the current thread while it downloads so you should use an AsyncTask

brthornbury
  • 3,518
  • 1
  • 22
  • 21
0

Assuming you have the picture url, here is a code sample that can download the picture and assign it to the ImageView

Drawable drawable = LoadImageFromWebOperations(enclosure.getUrl());
if (drawable!=null)
img.setImageDrawable(drawable);
img.setScaleType(ScaleType.CENTER_CROP); // Used to crop the picture in case it is too big


public Drawable LoadImageFromWebOperations(String url) {
    try{
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    }catch (Exception e) {
        return null;
    }

I would recommend you to insert this code in an ASyncTask, because it WILL download the picture, and such operations are not intended to be done on the main thread. If you won't add an ASyncTask, the UI will freeze.

RE6
  • 2,684
  • 4
  • 31
  • 57