我想显示外部图像,如:
在我的安卓手机应用程序中。
谁能指导我如何实现这一目标?
有很多方法可以实现您的要求。基本上,您必须使用 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 上使用它
从 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();
}