【发布时间】:2011-01-08 09:03:37
【问题描述】:
在尝试从 URL 显示 JPG 时,我在让图像出现在我的 ImageView 中时遇到问题。我能够打开一个 URLConnection,将图像拉到 InputStream 中,将该流解码为位图。我什至可以获取位图的高度和宽度值。我将位图设置为 ImageView 并且仍然可以获得 Drawable 的高度。但是,图像仍然没有出现在我的应用程序中。关于我可能遗漏的任何想法?感谢您的帮助。
try{
URL imgURL = new URL(imgLocation);
URLConnection conn = imgURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is, 25);
Bitmap bm = BitmapFactory.decodeStream(bis);
if(bm != null){
System.err.println("Image Height: " + bm.getHeight());
System.err.println("Image Width: " + bm.getWidth());
} else {
System.err.println("bm is null!!!");
}
img.setImageBitmap(bm);
System.err.println("Drawable Height: " + img.getDrawable().getIntrinsicHeight());
} catch (IOException e) {
// Print out the exception that occurred
e.printStackTrace();
}
【问题讨论】: