我正在尝试打开JPEG图像的远程Stream并将其转换为Bitmap对象:
BitmapFactory.decodeStream( new URL("http://some.url.to/source/image.jpg") .openStream());
解码器返回null,在日志中我得到以下消息:
DEBUG/skia(xxxx): --- decoder->decode returned false
注意:
1.内容长度不为零,内容类型为image / jpeg
2.当我在浏览器中打开URL时,我可以看到图像.
我在这里失踪的是什么?
请帮忙.谢谢.
解决方法
android bug n°6066中提供的解决方案包括重写std FilterInputStream,然后将其发送到BitmapFactory.
static class FlushedInputStream extends FilterInputStream { public FlushedInputStream(InputStream inputStream) { super(inputStream); } @Override public long skip(long n) throws IOException { long totalBytesSkipped = 0L; while (totalBytesSkipped < n) { long bytesSkipped = in.skip(n - totalBytesSkipped); if (bytesSkipped == 0L) { int byteValue = read(); if (byteValue < 0) { break; // we reached EOF } else { bytesSkipped = 1; // we read one byte } } totalBytesSkipped += bytesSkipped; } return totalBytesSkipped; } }
然后使用decodeStream函数:
Bitmap bitmap = BitmapFactory.decodeStream(new FlushedInputStream(inputStream));
我发现的另一个解决方案是简单地给BitmapFactory一个BufferedInputStream:
Bitmap bitmap = BitmapFactory.decodeStream(new BufferedInputStream(inputStream));
这两种解决方案应该可行.
更多信息可以在错误报告评论中找到:android bug no.6066