2010-05-07 35 views
7

我试图打开一个JPEG图像的远程流,并将其转换成位图对象:Skia的解码器解码失败时,远程流

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。

这是什么我在这里失踪?

请帮忙。谢谢。

+0

你能提供一个链接到一个图像,表现出这种行为? – CommonsWare 2010-05-07 11:26:15

+0

不幸的是,我不能。我明白你在这里间接暗示...... :) – Samuh 2010-05-08 04:45:07

回答

3

似乎有一些流的问题和android处理它的方式;这个bug report中的补丁现在解决了这个问题。

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)); 

其他解决方案,我发现的是,轻易地放弃一个的BufferedInputStream到TH BitmapFactory:

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

这两个解决方案应该做到这一点。

更多信息可以在错误报告的意见中找到:android bug no.6066

+2

FWIW FlushedInputStream方法适用于我; BufferedInputStream没有。此外,“字节”是一个保留关键字,所以您应该将“int byte”更改为“int byteValue”或其他。 – esilver 2011-10-28 05:48:48

+0

该解决方案适用于我,我认为这应该是正确的答案。 – KaHeL 2012-07-26 03:43:51

+1

这个错误仍然没有解决,变通办法也不总是有效。 – slybloty 2012-10-25 14:14:29

0

对我来说,问题是与类型的图像颜色的:你的图片是彩色= CYMK不是RGB

0

我已经找到了库,可以打开Android SKIA失败的图像。它可以对某些usecases有用:

https://github.com/suckgamony/RapidDecoder

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