当某些字节错误时 BitmapFactory.decodeStream(inputStream) 总是返回 null
我正在构建一个 Android 应用程序,目前无法从 URL 检索位图。这是我正在使用的代码:
public static Bitmap bmpFromURL(URL imageURL){
Bitmap result = null;
try {
HttpURLConnection connection = (HttpURLConnection)imageURL .openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
result = BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
当图片写入时一切正常,但当某些字节错误时,结果为空。我认为这基本上是可以预料的,因为它是在 BitmapFactory.decodeStream 的文档中写的:
如果输入流为 null,或者无法用于解码位图,则该函数返回 null。流的位置将是读取编码数据后的位置。
问题是,我的网络浏览器可以很好地解释我的错误图片,并且我可以在 iPhone 平台上这样做。
有没有办法忽略那些错误的像素?也许与选项参数?
任何帮助感谢
罗曼
I'm building an android app and I'm currently having trouble retrieving a bitmap from an URL. Here is the code I'm using :
public static Bitmap bmpFromURL(URL imageURL){
Bitmap result = null;
try {
HttpURLConnection connection = (HttpURLConnection)imageURL .openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
result = BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
Everything works fine when the picture's write but when some bytes are wrong, result gets null. I think it's basically expectable as it's written this in the doc of BitmapFactory.decodeStream
:
If the input stream is null, or cannot be used to decode a bitmap, the function returns null. The stream's position will be where ever it was after the encoded data was read.
The problem is, my wrong picture is well interpreted by my web browser and I can do so on iPhone platform.
Is there a way to sort of ignore those wrong pixels? maybe with the option parameter?
Any help appreciated
Romain
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 Android 未来版本中修复的已知错误。您可以通过首先将 InputStream 的内容复制到 byte[] 数组中,然后解码字节数组本身来解决此问题。
This is a known bug fixed in a future version of Android. You can work around it by first copying the content of the InputStream into a byte[] array and then decoding the byte array itself.
试试这个..它对我有用。希望有帮助。
Try this one out..It worked for me. hope it helps.