Volley Image Loader抛出ParseError

时间:2014-03-05 12:57:52

标签: android image loader android-volley

我使用NetworkImageView遇到了烦人的问题。它会在ParseError库方法中抛出ImageRequest.doParse(NetworkResponse response)

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 感觉XE
  • Nexus 5
  • 三星 S4
  • 三星注意

并且只有Samsung Note出现此错误。

网址没问题 - 我可以在浏览器中打开它(是的,这是一张图片)
数据应该没问题,因为它可以在其他设备中解码

decodeByteArray documantaion说:

  

返回
  解码后的位图,如果图像数据无法解码,则为null;如果opts为非> null,则为opts请求仅返回大小(在opts.outWidth和opts.outHeight中)

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

由于

1 个答案:

答案 0 :(得分:2)

问题出在图片格式上。这是 JFIF 。似乎并非所有设备都支持它。因为我可以访问服务器端,所以我只是在那里更改了Image。其他方式您需要实现自己的解码方法以在所有设备中支持此格式。

干杯