5

how to read image from particular url in android?

user538626
  • 911
  • 2
  • 11
  • 15
  • Refer [this page....](http://stackoverflow.com/questions/2471935/how-to-load-an-imageview-by-url-in-android) It will be helpful.. Happy Coding...!!!! – Dhasneem Apr 24 '13 at 13:34

2 Answers2

11

Use this

URL url = new URL(imageUrl);
HttpURLConnection connection  = (HttpURLConnection) url.openConnection();

InputStream is = connection.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is);  

imageView.setImageBitmap(img );
Labeeb Panampullan
  • 34,521
  • 28
  • 94
  • 112
5

Below code should help you for reading image. But remember that if you do this in UI Thread then it hangs UI. You should always open a new thread and load image in that thread. So your app always remain responsive.

InputStream is = null;
BufferedInputStream bis = null;
Bitmap bmp = null;
try {
   URLConnection conn = url.openConnection();
   conn.connect();
   is = conn.getInputStream();
   bis = new BufferedInputStream( is );
   bmp = BitmapFactory.decodeStream( bis );
} catch (MalformedURLException e) {

} catch (IOException e) {

} finally {
   try {
      is.close();
      bis.close();
   } catch (IOException e) {

   }
}
imageView.setImageBitmap( bmp );
Umakant Patil
  • 2,227
  • 6
  • 32
  • 58