BitmapFactory返回null Android

时间:2012-06-27 08:38:05

标签: android scale bitmapfactory

好的,所以我下载一个图像并将输入流发送到这个方法巫婆必须解码它一次找到图像大小然后计算一个比例值然后从流创建一个迷你版的位图....但我在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);

}

2 个答案:

答案 0 :(得分:1)

原因是:您使用了两次的流

options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FlushedInputStream(is), null, options);

再次:

 options.inJustDecodeBounds = false;
        options.inSampleSize = scale;
return BitmapFactory.decodeStream(new FlushedInputStream(is), null, options);

所以如果你想避免这种情况,你需要关闭流并再次重新打开流。

答案 1 :(得分:0)

如何直接提供InputStream?

return BitmapFactory.decodeStream(is, null, options);