如何在安卓系统中显示外部图片?

3
我想在我的Android手机应用程序中显示外部图片,类似于:
"http://abc.com/image.jpg"
有没有人可以指导我如何实现这个功能?
2个回答

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等组件上。


3

另一种从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();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接