how to display external image in android?

南楼画角 提交于 2019-11-30 16:19:35

问题


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?


回答1:


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




回答2:


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();
}


来源:https://stackoverflow.com/questions/2989077/how-to-display-external-image-in-android

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