好的,所以我下载了一张图片,并将输入流发送到这个方法,它必须解码一次以找到图像大小,然后计算比例值,然后根据流创建一个迷你版本的位图……但是我在logcat中得到了错误,bitmapFactory返回null,有人知道会出什么问题吗?
public static Bitmap getSampleBitmapFromStream(InputStream is,
int reqWidth, int reqHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FlushedInputStream(is), null, options);
// Find the correct scale value. It should be the power of 2.
int width_tmp = options.outWidth, height_tmp = options.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < reqWidth || height_tmp / 2 < reqHeight)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// decode with inSampleSize
options.inJustDecodeBounds = false;
options.inSampleSize = scale;
return BitmapFactory.decodeStream(new FlushedInputStream(is), null, options);
}发布于 2012-06-27 16:45:45
原因是:您使用了两次的流
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FlushedInputStream(is), null, options);再说一遍:
options.inJustDecodeBounds = false;
options.inSampleSize = scale;
return BitmapFactory.decodeStream(new FlushedInputStream(is), null, options);因此,如果您想避免这种情况,您需要关闭流,然后重新打开流。
发布于 2012-06-27 16:40:57
直接提供InputStream怎么样?
return BitmapFactory.decodeStream(is, null, options);https://stackoverflow.com/questions/11222362
复制相似问题