Volley 图片加载器抛出 ParseError 错误

3
我遇到了使用NetworkImageView时麻烦的问题。在ImageRequest.doParse(NetworkResponse response)库方法中,它会抛出ParseError异常。
private Response<Bitmap> doParse(NetworkResponse response) {
    byte[] data = response.data;
    BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
    Bitmap bitmap = null;
    if (mMaxWidth == 0 && mMaxHeight == 0) {
        decodeOptions.inPreferredConfig = mDecodeConfig;
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
    } else {
        // If we have to resize this image, first get the natural bounds.
        decodeOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
        int actualWidth = decodeOptions.outWidth;
        int actualHeight = decodeOptions.outHeight;

        // Then compute the dimensions we would ideally like to decode to.
        int desiredWidth = getResizedDimension(mMaxWidth, mMaxHeight,
                actualWidth, actualHeight);
        int desiredHeight = getResizedDimension(mMaxHeight, mMaxWidth,
                actualHeight, actualWidth);

        // Decode to the nearest power of two scaling factor.
        decodeOptions.inJustDecodeBounds = false;
        // TODO(ficus): Do we need this or is it okay since API 8 doesn't support it?
        // decodeOptions.inPreferQualityOverSpeed = PREFER_QUALITY_OVER_SPEED;
        decodeOptions.inSampleSize =
            findBestSampleSize(actualWidth, actualHeight, desiredWidth, desiredHeight);
        Bitmap tempBitmap =
            BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);

        // If necessary, scale down to the maximal acceptable size.
        if (tempBitmap != null && (tempBitmap.getWidth() > desiredWidth ||
                tempBitmap.getHeight() > desiredHeight)) {
            bitmap = Bitmap.createScaledBitmap(tempBitmap,
                    desiredWidth, desiredHeight, true);
            tempBitmap.recycle();
        } else {
            bitmap = tempBitmap;
        }
    }

    if (bitmap == null) {
        return Response.error(new ParseError(response));
    } else {
        return Response.success(bitmap, HttpHeaderParser.parseCacheHeaders(response));
    }
}

出现此问题是因为:

bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);

返回了null。但并非所有设备都会返回null。

我已在以下设备上进行了测试:

  • HTC Sensation XE
  • Nexus 5
  • Samsung S4
  • Samsung Note

只有三星Note出现了这个错误。

Url正常——我可以在浏览器中打开它(确实是图片)
Data应该没问题,因为它可以在其他设备上解码

decodeByteArray文档说明如下:

返回值
已解码的位图;如果无法解码图像数据,则返回null;或者,如果opts为非空,则返回opts.outWidth和opts.outHeight中请求大小的结果(仅请求大小)

我已尝试将opts参数传递为null,但它仍然返回null。那么,有人知道这里发生了什么吗?

谢谢

1个回答

2
问题出在图像格式上。它是JFIF格式。似乎并非所有设备都支持它。由于我可以访问服务器端,所以我只需在那里更改图像即可。另一种方法是实现自己的解码方法以支持所有设备中的此格式。
祝好!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接