0

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

}
4

2 回答 2

2

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

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-27T08:45:45.297 回答
0

直接提供 InputStream 怎么样?

return BitmapFactory.decodeStream(is, null, options);
于 2012-06-27T08:40:56.590 回答