0

Possible Duplicate:
how to load an imageview by url in android?

I am having an image URL.I want to set that image as my ImageViews background. Please help..

Community
  • 1
  • 1
yshak
  • 1,991
  • 2
  • 16
  • 14

2 Answers2

1

I Hope This code Help you.

  public static Bitmap loadBitmap(String url) {
    Bitmap bitmap = null;
    InputStream in = null;
    BufferedOutputStream out = null;

    try {
        in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);

        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
        copy(in, out);
        out.flush();

        final byte[] data = dataStream.toByteArray();
        BitmapFactory.Options options = new BitmapFactory.Options();
        //options.inSampleSize = 1;

        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
    } catch (IOException e) {
        Log.e(TAG, "Could not load Bitmap from: " + url);
    } finally {
        closeStream(in);
        closeStream(out);
    }

    return bitmap;
}

Then Use imageView.setImageBitmap(bitmap);

Hasan Khan
  • 554
  • 1
  • 7
  • 23
Dhaval Khant
  • 2,467
  • 2
  • 16
  • 24
0

Check this Link How to display image from URL on Android

You can then use setBackgroundDrawable() with the drawable created from the URL.

Community
  • 1
  • 1
Karthik
  • 3,509
  • 1
  • 20
  • 26