how to display external image in android?

你离开我真会死。 提交于 2019-11-30 16:07:43
Francesco Laurita

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

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();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!