我遇到了使用
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。那么,有人知道这里发生了什么吗?
谢谢