Adding Images to Image View through Image URL – Android

ImageView img = (ImageView) findViewById(R.id.imageview1);

try {
URL url = new URL("your image url here");
//try this url :- http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg

HttpGet httpRequest = null;
httpRequest = new HttpGet(url.toURI());
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient
.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
InputStream input = b_entity.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(input);
img.setImageBitmap(bitmap);
}
catch (Exception ex) {

Toast t2 = Toast.makeText(getApplicationContext(),
                "Image Loading Failed", Toast.LENGTH_LONG);
        t2.setGravity(Gravity.CENTER, 0, 0);
        t2.show();
}

Here we are getting the image from the url as a InputStream, and converting it to Bitmap and feeding into the
ImageView img. If there was any error the toast message will display the message “Image Loading Failed”.

Hope you guys understood it

Download Sample Project

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s