BitmapFactory.decodeStream returning null when options are set.
- by Robert Foss
Hi!
I'm having issues with BitmapFactory.decodeStream(). When using it without options, it will return an image. But when I use it with options as in .decodeStream(inputStream, null, options) it never returns Bitmaps.
What I'm trying to do is to downsample a Bitmap before I actually load it to save memory.
I've read some good guides, but none using .decodeStream.
Here
httpIM:NOT//nornalbion.SPAMcom/blog/?p=143
httpIM:NOT//kfb-android.blogspot.SPAMcom/2009/04/image-processing-in-android.html
WORKS JUST FINE
InputStream is = connection.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is, null, options);
DOESNT WORK
InputStream is = connection.getInputStream();
Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, options);
Boolean scaleByHeight = Math.abs(options.outHeight - TARGET_HEIGHT) >= Math.abs(options.outWidth - TARGET_WIDTH);
if(options.outHeight * options.outWidth * 2 >= 200*100*2){
// Load, scaling to smallest power of 2 that'll get it <= desired dimensions
double sampleSize = scaleByHeight
? options.outHeight / TARGET_HEIGHT
: options.outWidth / TARGET_WIDTH;
options.inSampleSize =
(int)Math.pow(2d, Math.floor(
Math.log(sampleSize)/Math.log(2d)));
System.out.println("Samplesize: " + options.inSampleSize);
}
// Do the actual decoding
options.inJustDecodeBounds = false;
//options.inTempStorage = new byte[IMG_BUFFER_LEN];
Bitmap img = BitmapFactory.decodeStream(is, null, options);