How to display external image in android?

I want to display an external image, for example:

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

in my android phone app.

Can anyone advise me how to do this?

+3
source share
2 answers

There are many ways to reach your request. Basically you need to load an image using urlrequest and then use an InputStream to create a Bitmap object.

Just an example 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 receiving the Bitmap object, you can use it in your ImageView, for example

+6
source

Another approach for loading an 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();
}
+3
source

Source: https://habr.com/ru/post/1748831/


All Articles