Skia Decoder fails to decode remote Stream

2019-02-13 07:29发布

I am trying to open a remote Stream of a JPEG image and convert it into a Bitmap object:

    BitmapFactory.decodeStream(
new URL("http://some.url.to/source/image.jpg")
.openStream());

The decoder returns null and in the logs I get the following message:

DEBUG/skia(xxxx): --- decoder->decode returned false

Note:
1. the content length is non-zero and content type is image/jpeg
2. When I open the URL in browser I can see the image.

What is that I am missing here?

Please help. Thanks.

4条回答
爷的心禁止访问
2楼-- · 2019-02-13 08:10

seems there was some problem with the stream and the way android handled it; the patch in this bug report solved the problem for now.

查看更多
可以哭但决不认输i
3楼-- · 2019-02-13 08:10

For me the problem is with type of color of image: your image are in color= CYMK not in RGB

查看更多
Summer. ? 凉城
4楼-- · 2019-02-13 08:18

The solution provided in android bug n°6066 consist in overriding the std FilterInputStream and then send it to the 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;
    }
}

and then use the decodeStream function:

Bitmap bitmap = BitmapFactory.decodeStream(new FlushedInputStream(inputStream));

The other solution i've found is to simply give a BufferedInputStream to th BitmapFactory:

Bitmap bitmap = BitmapFactory.decodeStream(new BufferedInputStream(inputStream));

These two solutions should do the trick.

More information can be found in the bug report comments : android bug no.6066

查看更多
\"骚年 ilove
5楼-- · 2019-02-13 08:24

I have found a library, which can open images on which Android SKIA fails. It can be useful for certain usecases:

https://github.com/suckgamony/RapidDecoder

For me it solved the problem as I am not loading many images at once and lot of images I load have ICC profile. I haven't tried integrating it with some common libraries like Picasso or Glide.

查看更多
登录 后发表回答