BitmapFactory.decodeStream(InputStream is) 对于 Android 上的非 null InputStream 返回 null
我正在开发一个 Android 应用程序,它的视图包含多个图库。图库的内容(位图)是来自互联网的红色内容。
对于第一个图库,一切正常,但是当尝试下载第二个图库的第一张图像时,BitmapFactory.decodeStream(InputStream)
返回 null,而流不为 null。
public void loadBitmap() throws IOException {
for (int i = 0; i < images.size(); ++i) {
URL ulrn = new URL(images.get(i).getThumbUrl());
HttpURLConnection con = (HttpURLConnection) ulrn.openConnection();
InputStream is = con.getInputStream();
images.get(i).setImage(BitmapFactory.decodeStream(is));
Log.i("MY_TAG", "Height: " + images.get(i).getImage().getHeight());
}
}
getThumbUrl()
返回图像的 URL(例如 http://mydomain.com/image.jpg< /a>) 它会在 Log.i("MY_TAG", "Height: ... )
行抛出一个 NullPointerException
(images
是一个 ArrayList
,包含我的类的对象,它也包含 URL 和位图)。
感谢您的任何建议!
I'm developing an Android application, and it's view is containing multiple Gallerys. The content of the Gallerys (the Bitmaps) are red from the Internet.
For the first gallery, everything works fine, but when trying to download the first image of the second Gallery, the BitmapFactory.decodeStream(InputStream)
returns null, while the stream is NOT null.
public void loadBitmap() throws IOException {
for (int i = 0; i < images.size(); ++i) {
URL ulrn = new URL(images.get(i).getThumbUrl());
HttpURLConnection con = (HttpURLConnection) ulrn.openConnection();
InputStream is = con.getInputStream();
images.get(i).setImage(BitmapFactory.decodeStream(is));
Log.i("MY_TAG", "Height: " + images.get(i).getImage().getHeight());
}
}
The getThumbUrl()
returns the URL of the image (eg. http://mydomain.com/image.jpg)
and it throws a NullPointerException
at the line Log.i("MY_TAG", "Height: ... )
(images
is an ArrayList
containing objects of my class, that holds the URL and the Bitmap too).
Thanks for any advice!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到过这个。尝试将 BufferedHttpEntity 与您的输入流一起使用。我发现这可以防止 99.9% 的从解码流获取静默空值的问题。
也许并不重要,但我可靠地使用 org.apache.http.client.HttpClient 而不是 HttpURLConnection,如下所示:
I've run into this. Try using BufferedHttpEntity with your inputstream. I found this prevented 99.9% of the issues with getting silent nulls from decodeStream.
Maybe not signficant, but I reliably use org.apache.http.client.HttpClient rather than HttpURLConnection as in:
谷歌把我带到了这里。对于遇到同样问题的每个人:
问题:
http://code.google.com/p/android/issues/detail ?id=6066
解决方案(“FlushedInputStream”):
http://android-developers.blogspot.com/2010/07 /多线程-for-performance.html
Google brought me here. For everyone having the same problem:
Problem:
http://code.google.com/p/android/issues/detail?id=6066
Solution ("FlushedInputStream"):
http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html
返回
解码后的位图,如果图像数据无法解码,则返回 null。
您是否检查过没有收到 404 错误或类似错误而不是图像?
Returns
The decoded bitmap, or null if the image data could not be decoded.
Did you check that you're not getting some 404 error or similar instead of the image?