Android load image view from url

by mariganesh 2014-04-19 21:00:07


Android load image view from url

Use the following code to load imageview from url using by bitmap


private Bitmap downloadBitmap(String url) {
// initilize the default HTTP client object
final DefaultHttpClient client = new DefaultHttpClient();

//forming a HttoGet request
final HttpGet getRequest = new HttpGet(url);
try {

HttpResponse response = client.execute(getRequest);

//check 200 OK for success
final int statusCode = response.getStatusLine().getStatusCode();

if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "Error " + statusCode +
" while retrieving bitmap from " + url);
return null;

}

final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
// getting contents from the stream
inputStream = entity.getContent();

// decoding stream data back into image Bitmap that android understands
image = BitmapFactory.decodeStream(inputStream);


} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (Exception e) {
// You Could provide a more explicit error message for IOException
getRequest.abort();
Log.e("ImageDownloader", "Something went wrong while" +
" retrieving bitmap from " + url + e.toString());
}

return image;
}

697
like
0
dislike
0
mail
flag

You must LOGIN to add comments