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