3

I want to display external image like:

"http://abc.com/image.jpg"

in my android phone application.

can any one guide me how to achieve this?

Janusz
  • 187,060
  • 113
  • 301
  • 369
UMAR-MOBITSOLUTIONS
  • 77,236
  • 95
  • 209
  • 278

2 Answers2

6

There are many ways to achieve your request. Basically you have to download the image with an urlrequest and then using the InputStream to create a Bitmap object.

Just a sample code:

URL url = new URL("http://asd.jpg");
        URLConnection conn = url.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();


        BufferedInputStream bis = new BufferedInputStream(is);

        Bitmap bm = BitmapFactory.decodeStream(bis);

        bis.close();
        is.close();

After you obtain the Bitmap object you can use it on your ImageView for instance

Janusz
  • 187,060
  • 113
  • 301
  • 369
Francesco Laurita
  • 23,434
  • 8
  • 55
  • 63
3

Just another approach to download the image from a url

try {
  Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://abc.com/image.jpg").getContent());
} catch (MalformedURLException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}
Primal Pappachan
  • 25,857
  • 22
  • 67
  • 84