Skia Decoder fails to decode remote Stream

房东的猫 提交于 2019-11-30 20:07:09
ol_v_er

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

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!