开发者

how to display external image in android?

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开发者_StackOverflow社区 this?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜