setImageURI/setimagebitmap从网络中获取图像并在图像视图中显示
问题描述:
我想从网络中从某个URL获取图像并将其显示在我的ImageView中。 以下是我使用的代码: -setImageURI/setimagebitmap从网络中获取图像并在图像视图中显示
Bitmap bm = null;
try {
URL aURL = new URL("stringURL");
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
}catch (Exception e) {
// TODO: handle exception
}
iv.setImageBitmap(bm);
,但我不能让图像
答
我认为错误是
URL aURL = new URL("stringURL");
应该是不带引号
URL aURL = new URL(stringURL);
如果stringURL是有效的url它将工作...
希望它有帮助../
答
我觉得你可以使用下面的代码,这对我很好。
String stringURL = "Your url here";
InputStream is = null;
BufferedInputStream bis = null;
Bitmap bmp = null;
try {
URL url = new URL(stringURL);
URLConnection conn = url.openConnection();
conn.connect();
is = conn.getInputStream();
bis = new BufferedInputStream(is);
bmp = BitmapFactory.decodeStream(bis);
} catch (MalformedURLException e) {
} catch (IOException e) {
}catch (Exception e) {
} finally {
try {
if(is != null)
is.close();
if(bis != null)
bis.close();
} catch (IOException e) {
}
}
iv.setImageBitmap(bmp);