views:

1932

answers:

1

for example i have an image's url:

http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png

i want to load this link image into my imageview in my project.how?

+2  A: 

You'll have to download the image firstly

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 the Imageview.setImageBitmap to set bitmap into the ImageView

steve
u r right. but just these 3 lines helps to solve the problem.URL newurl = new URL(photo_url_str); mIcon_val = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream()); profile_photo.setImageBitmap(mIcon_val); thanks for ur reply.
Praveen Chandrasekaran
For URL - import java.net.URL;private static final int IO_BUFFER_SIZE = 4 * 1024;what was the last one?
steve
See the code here: http://stackoverflow.com/questions/3118691/android-make-an-image-at-a-url-equal-to-imageviews-image
OneWorld