位图未出现在 ImageView 中
当尝试从 URL 显示 JPG 时,我在使图像显示在 ImageView 中时遇到问题。我能够打开 URLConnection,将图像拉入输入流中,将该流解码为位图。我什至可以获得位图的高度和宽度值。我将位图设置为 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();
}
While trying to display a JPG from a URL I'm having an issue getting the image to appear in my ImageView. I'm able to open a URLConnection, pull the image down in an InputStream, decode that stream into Bitmap. I can even get values for the height and width of the bitmap. I set the bitmap to the ImageView and can still get a height for the Drawable. However, the image still does not appear in my application. Any ideas of what I could be missing? Thanks for any help.
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法直接回答您的问题,但我可以建议针对此需求的替代解决方案。
使用 ImageView 会给你带来很多可用性问题。
您需要避免屏幕锁定,从优化角度来看,重新下载图像是不好的。
我建议更简单&高效的解决方案;您可以使用 Droid-Fu 的 WebImageView。
它就像一个魅力。您只需传递图像的 url,然后让 WebImageView 处理下载、显示进度甚至缓存。
这是 GitHub 上的 Droid-Fu 的 playgorund(搜索“droid-fu”)
ps 我还对此缓存机制进行了一些扩展,已经在我的 github Playground 中发布了(搜索“wareninja”)
I don't have answer directly to your question, but I can suggest alternative solution for this need.
Using ImageView will cause you lots of issues, for usability.
You need to avoid screen locking, and re-dowloading image is bad from optimization perspective.
I would suggest much simpler & efficient solution; you can use WebImageView from Droid-Fu.
it works like a charm. you just pass the url of the image, and you let WebImageView handle downloading, showing progress and even caching.
here is Droid-Fu's playgorund on GitHub (search for 'droid-fu')
p.s. I also did some extensions to this caching mechanism, already released in my github playground (search for 'wareninja')