7

我正在尝试打开 JPEG 图像的远程流并将其转换为位图对象:

    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 时,我可以看到图像。

我在这里想念什么?

请帮忙。谢谢。

4

4 回答 4

10

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

于 2011-03-22T18:19:28.683 回答
3

似乎流和android处理它的方式有问题;这个错误报告中的补丁现在解决了这个问题。

于 2010-05-09T03:53:01.460 回答
0

对我来说,问题在于图像的颜色类型:您的图像是颜色 = CYMK 而不是 RGB

于 2013-12-10T13:11:09.107 回答
0

我找到了一个库,它可以打开 Android SKIA 失败的图像。它对某些用例很有用:

https://github.com/suckgamony/RapidDecoder

对我来说,它解决了这个问题,因为我没有一次加载很多图像,而且我加载的很多图像都有 ICC 配置文件。我还没有尝试将它与 Picasso 或 Glide 等一些常用库集成。

于 2017-09-01T16:25:26.700 回答