3

此处测试图片:http: //images.plurk.com/tn_4134189_bf54fe8e270ce41240d534b5133884ee.gif

我尝试了在互联网上找到的几种解决方案,但没有有效的解决方案。

我正在使用以下代码段:

Url imageUrl = new Url("http://images.plurk.com/tn_4134189_bf54fe8e270ce41240d534b5133884ee.gif");
Bitmap image = BitmapFactory.decodeStream(imageUrl.openStream());

总是得到这个日志:

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

有什么帮助吗?谢谢。

编辑:

那些未能解码的图像也无法显示在WebView. 但可以查看是否在浏览器中打开。

4

9 回答 9

4

我有同样的问题,部分由这个类修复:

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 byte = read();
              if (byte < 0) {
                  break;  // we reached EOF
              } else {
                  bytesSkipped = 1; // we read one byte
              }
       }
        totalBytesSkipped += bytesSkipped;
    }
    return totalBytesSkipped;
}

}

和:

InputStream in = null;
    try {
        in = new java.net.URL(imageUrl).openStream();
        } catch (MalformedURLException e) {
        e.printStackTrace();
        } catch (IOException e) {
        e.printStackTrace();
        }
Bitmap image = BitmapFactory.decodeStream(new FlushedInputStream(in));

它在大多数情况下都有帮助,但这不是通用的解决方案。有关更多信息,请参阅此错误报告

好运!

于 2010-10-20T15:18:08.577 回答
4

尝试将此作为临时解决方法:

首先添加以下类:

  public static class PlurkInputStream extends FilterInputStream {

    protected PlurkInputStream(InputStream in) {
        super(in);
    }

    @Override
    public int read(byte[] buffer, int offset, int count)
        throws IOException {
        int ret = super.read(buffer, offset, count);
        for ( int i = 2; i < buffer.length; i++ ) {
            if ( buffer[i - 2] == 0x2c && buffer[i - 1] == 0x05
                && buffer[i] == 0 ) {
                buffer[i - 1] = 0;
            }
        }
        return ret;
    }

}

然后用 PlurkInputStream 包装你的原始流:

Bitmap bitmap = BitmapFactory.decodeStream(new PlurkInputStream(originalInputStream));

让我知道这是否对您有帮助。

编辑:

抱歉,请尝试以下版本:

        for ( int i = 6; i < buffer.length - 4; i++ ) {
            if ( buffer[i] == 0x2c ) {
                if ( buffer[i + 2] == 0 && buffer[i + 1] > 0
                    && buffer[i + 1] <= 48 ) {
                    buffer[i + 1] = 0;
                }
                if ( buffer[i + 4] == 0 && buffer[i + 3] > 0
                    && buffer[i + 3] <= 48 ) {
                    buffer[i + 3] = 0;
                }
            }
        }

请注意,这不是有效的代码,也不是完整/正确的解决方案。它适用于大多数情况,但不是全部。

于 2010-10-28T06:08:26.837 回答
1

我尝试了所有解决方案,但没有解决我的问题。经过一些测试,当互联网连接不稳定时,skia解码器失败的问题发生了很多。对我来说,强制重新下载图像解决了这个问题。

当图像尺寸较大时,该问题也更加突出。

使用循环最多需要我重试 2 次,并且图像将被正确下载。

Bitmap bmp = null;
int retries = 0;
while(bmp == null){
    if (retries == 2){
        break;
    }
    bmp = GetBmpFromURL(String imageURL);
    Log.d(TAG,"Retry...");
    retries++;
}
于 2012-08-08T18:50:24.873 回答
0

这应该有效:

URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
connection.disconnect();
input.close();

myBitmap 包含您的图像。

于 2010-09-27T11:14:38.307 回答
0

这是由于 Android 中 InputStream 类中的一个错误。您可以在此处找到有效的解决方法和错误描述http://code.google.com/p/android/issues/detail?id=6066

于 2010-10-21T21:25:41.703 回答
0

试试这个:

HttpGet httpRequest = new HttpGet(url);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(entity);
InputStream is = bufferedHttpEntity.getContent();
Drawable d = Drawable.createFromStream(is, "");
//or bitmap
//Bitmap b = BitmapFactory.decodeStream(is);
于 2011-01-21T11:18:24.627 回答
0

出于内存原因,您必须像这样实现 BitmapFactory 选项:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4; // might try 8 also

主要的下载位图功能可能是这样的:

Bitmap downloadBitmap(String url) {

    final HttpClient client = AndroidHttpClient.newInstance("Android");
    final HttpGet getRequest = new HttpGet(url);

    try {
        HttpResponse response = client.execute(getRequest);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            if(DEBUG)Log.w("ImageDownloader", "Error " + statusCode +
                    " while retrieving bitmap from " + url);
            return null;
        }

        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {

                inputStream = entity.getContent();
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 4; // might try 8 also
                return BitmapFactory.decodeStream(new FlushedInputStream(inputStream),null,options);

            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                entity.consumeContent();
            }
        }
    } catch (IOException e) {
        getRequest.abort();
        if(DEBUG)Log.w(TAG, "I/O error while retrieving bitmap from " + url, e);
    } catch (IllegalStateException e) {
        getRequest.abort();
        if(DEBUG)Log.w(TAG, "Incorrect URL: " + url);
    } catch (Exception e) {
        getRequest.abort();
        if(DEBUG)Log.w(TAG, "Error while retrieving bitmap from " + url, e);
    } finally {
        if ((client instanceof AndroidHttpClient)) {
            ((AndroidHttpClient) client).close();
        }
    }
    return null;
}

也许你必须像这样实现 AsyncTask: http ://android-developers.blogspot.com/2010/07/multithreading-for-performance.html

于 2012-05-15T17:40:53.310 回答
0

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

于 2013-12-10T12:43:32.490 回答
0

也许这不是您的情况,但如果您尝试使用 CMYK 颜色空间而不是 RGB 颜色空间解码图像,则可能是这种情况。CMYK 图像,像这样的,Android 不支持,即使在 Android 网络浏览器中也不会显示。在此处阅读有关此内容的更多信息:

无法使用 BitmapFactory.decodeFile 加载 JPEG 图像。返回空

于 2013-12-11T11:30:35.990 回答