user924941
user924941

Reputation: 1011

BitmapFactory returning null Android

Ok so i download a image and send the input stream to this method witch has to decode it once to find the image size then calculate a scale value then create a mini version of the bitmap from the stream.... but i get errors in logcat that the bitmapFactory is returning null, anyone have a idea what could be wrong ?

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);

}

Upvotes: 0

Views: 770

Answers (2)

idiottiger
idiottiger

Reputation: 5177

the reason is: the stream you used twice

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

and again:

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

so if you want avoid this, you need close the stream and reopen the stream again.

Upvotes: 2

ariefbayu
ariefbayu

Reputation: 21979

What about supplying the InputStream directly?

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

Upvotes: 0

Related Questions