如何在android中显示外部图像?

时间:2010-06-07 11:38:11

标签: android imageview

我想显示外部图像,如:

http://abc.com/image.jpg

在我的Android手机应用程序中。

任何人都可以指导我如何实现这个目标吗?

2 个答案:

答案 0 :(得分:6)

有很多方法可以实现您的请求。基本上你必须使用urlrequest下载图像,然后使用InputStream创建一个Bitmap对象。

只是示例代码:

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

获取Bitmap对象后,您可以在ImageView上使用它,例如

答案 1 :(得分:3)

另一种从网址下载图片的方法

try {
  Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://abc.com/image.jpg").getContent());
} catch (MalformedURLException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}