Search code examples
androidbitmapbitmapfactory

Using BitmapFactory.Options causes changes in BitmapFactory


What I wanted to achieve is being able to calculate the height and width of a Bitmap from an inputstream without actually changing BitmapFactory.Options

This is what I did:

private Boolean testSize(InputStream inputStream){
    BitmapFactory.Options Bitmp_Options = new BitmapFactory.Options();
    Bitmp_Options.inJustDecodeBounds = true;
    BitmapFactory.decodeResourceStream(getResources(), new TypedValue(), inputStream, new Rect(), Bitmp_Options);
    int currentImageHeight = Bitmp_Options.outHeight;
    int currentImageWidth = Bitmp_Options.outWidth;
    if(currentImageHeight > 200 || currentImageWidth > 200){
        Object obj = map.remove(pageCounter);
        Log.i("Page recycled", obj.toString());
        return true;
    }
    return false;
}

Now the main problem here is that it changes the BitmapFactory.Options to a state that can't decodeStream properly.

My question is there another way of resetting BitmapFactory.Options? or another possible solution?

Another method: (Note* that originalBitmap is null when the top method is applied)

This is was my original code:

Bitmap originalBitmap = BitmapFactory.decodeStream(InpStream);

Applying Deev's and Nobu Game's suggestion: (no change)

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = false;
    Bitmap originalBitmap = BitmapFactory.decodeStream(InpStream,null,options);

Solution

  • You are attempting to read from the same stream twice. A stream doesn't work as a byte array. Once you've read data from it, you cannot read it again unless you reset the stream position. You can try to call InputStream.reset() after your first call to decodeStream() but not all InputStreams support this method.