Decoding bitmaps in Android with the right size

AndroidMemoryBitmapSd Card

Android Problem Overview


I decode bitmaps from the SD card using BitmapFactory.decodeFile. Sometimes the bitmaps are bigger than what the application needs or that the heap allows, so I use BitmapFactory.Options.inSampleSize to request a subsampled (smaller) bitmap.

The problem is that the platform does not enforce the exact value of inSampleSize, and I sometimes end up with a bitmap either too small, or still too big for the available memory.

From http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize:

> Note: the decoder will try to fulfill > this request, but the resulting bitmap > may have different dimensions that > precisely what has been requested. > Also, powers of 2 are often > faster/easier for the decoder to > honor.

How should I decode bitmaps from the SD card to get a bitmap of the exact size I need while consuming as little memory as possible to decode it?

Edit:

Current source code:

BitmapFactory.Options bounds = new BitmapFactory.Options();
this.bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, bounds);
if (bounds.outWidth == -1) { // TODO: Error }
int width = bounds.outWidth;
int height = bounds.outHeight;
boolean withinBounds = width <= maxWidth && height <= maxHeight;
if (!withinBounds) {
	int newWidth = calculateNewWidth(int width, int height);
	float sampleSizeF = (float) width / (float) newWidth;
	int sampleSize = Math.round(sampleSizeF);
	BitmapFactory.Options resample = new BitmapFactory.Options();
	resample.inSampleSize = sampleSize;
	bitmap = BitmapFactory.decodeFile(filePath, resample);
}

Android Solutions


Solution 1 - Android

You are on the right track, however you are trying to do two things at once: read the file in and scale it to the appropriate size.

The first step is to read the file to a Bitmap slightly bigger than you require, using BitmapFactory.Options.inSampleSize to ensure that you do not consume excessive memory reading a large bitmap when all you want is a smaller thumbnail or screen resolution image.

The second step is to call Bitmap.createScaledBitmap() to create a new bitmap to the exact resolution you require.

Make sure you clean up after the temporary bitmap to reclaim its memory. (Either let the variable go out of scope and let the GC deal with it, or call .recycle() on it if you are loading lots of images and are running tight on memory.)

Solution 2 - Android

You may want to use inJustDecodeBounds. Set it to TRUE and load the file as it is.

The image won't be loaded into memory. But the outheight and outwidth properties of BitmapFactory.Options will contain the actual size params of the image specified. Calculate how much u want to subsample it. i.e. 1/2 or 1/4 or 1/8 etc. and assign 2/4/8 etc. accordingly to the inSampleSize.

Now set inJustDecodeBounds to FALSE and call BitmapFactory.decodeFile() to load the image of the exact size as calculated above.

Solution 3 - Android

First you need to sample the image to nearest sampling value so that bitmap become memory efficient, which you have described perfectly. Once it's done you can scale it up to fit your screen.

// This function taking care of sampling
backImage =decodeSampledBitmapFromResource(getResources(),R.drawable.back, width, height);

// This will scale it to your screen width and height. you need to pass screen width and height.
backImage = Bitmap.createScaledBitmap(backImage, width, height, false); 

Solution 4 - Android

Since the API already states that the sample size may or may not be honored. So I guess ur out of luck while using BitmapFactory .

But the way out would be using your own bitmap reader. But I would suggest that you stick with BitmapFactory as its fairly well tested and standardized.

I would try and not worry too much about little extra memory consumption, but try and find out why its not honoring the values. Sadly I have not idea about that :(

Solution 5 - Android

ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(filesPath), THUMBSIZE, THUMBSIZE))

You can directly set the THUMBSIZE depending upon your requirement,however i am not sure about the lower level implementation details,output image quality and its performance but i usually prefer it when i need to show thumbnail.

Solution 6 - Android

For those who are looking for exact size image from resources.

For exact width pass reqHight=0 and for exact height pass reqWidth=0

public static Bitmap decodeBitmapResource(Context context, int resId, int reqWidth, int reqHeight)
{
	final BitmapFactory.Options options = new BitmapFactory.Options();
	options.inJustDecodeBounds = true;
	BitmapFactory.decodeResource(context.getResources(), resId, options);
	float scale = 1;
	if (reqWidth != 0 && reqHeight == 0)
	{
		options.inSampleSize = (int) Math.ceil(options.outWidth / (float) reqWidth);
		scale = (float) options.outWidth / (float) reqWidth;
	}
	else if (reqHeight != 0 && reqWidth == 0)
	{
		options.inSampleSize = (int) Math.ceil(options.outHeight / (float) reqHeight);
		scale = (float) options.outHeight / (float) reqHeight;
	}
	else if (reqHeight != 0 && reqWidth != 0)
	{
		float x = (float) options.outWidth / (float) reqWidth;
		float y = (float) options.outHeight / (float) reqHeight;
		scale = x > y ? x : y;
	}
	reqWidth = (int) ((float) options.outWidth / scale);
	reqHeight = (int) ((float) options.outHeight / scale);
	options.inJustDecodeBounds = false;
	Bitmap tmp = BitmapFactory.decodeResource(context.getResources(), resId, options);
	Bitmap out = Bitmap.createScaledBitmap(tmp, reqWidth, reqHeight, false);
	tmp.recycle();
	tmp = null;
	return out;
}

Solution 7 - Android

Since I use inSampleSize I never face out of memory issues any more. So inSampleSize works quite stable for me. I would recommend you to double check the issue. The size may be not exacly the same as you requested. But it should be reduced anyway and there should be no more "Out of memory". I would also recommend to post your code snippet here, maybe there's something wrong with it.

Solution 8 - Android

As the100rabh said, if you use BitmapFactory you don't really have much choice. However, bitmap decoding is really simple, and depending on the scaling algorithm you want to use, it's usually fairly simple to scale it on-the-fly without having to read large parts of the original bitmap into memory (in general, you'll only need double the memory required for 1-2 lines of the original bitmap).

Solution 9 - Android

When inScaled flag is set (by default it is TRUE), if inDensity and inTargetDensity are not 0, the bitmap will be scaled to match inTargetDensity when loaded, rather than relying on the graphics system scaling it each time it is drawn to a Canvas. So simply set the inScaled to false will do.

BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;

Solution 10 - Android

I was able to approximately get the "right size" by rescaling the decoded image file to 70% its bitmap size.

Bitmap myBitmap = BitmapFactory.decodeFile(path);
if(myBitmap != null)
{
  //reduce to 70% size; bitmaps produce larger than actual image size
  Bitmap rescaledMyBitmap = Bitmap.createScaledBitmap(
                                                  myBitmap, 
		                    	                  myBitmap.getWidth()/10*7, 
		                    	                  myBitmap.getHeight()/10*7, 
                                                  false);

  image.setImageBitmap(rescaledMyBitmap);
}	             

I hope this helps you out somehow! Peace!

Solution 11 - Android

Documentation is very self-explaining. Firstly I thought to make a small clip out of there but found out that full read is better for understanding and required for not making some fails with memory. For the last, it's pretty small.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhpiqueView Question on Stackoverflow
Solution 1 - AndroidsirdodgerView Answer on Stackoverflow
Solution 2 - AndroidTheCodeArtistView Answer on Stackoverflow
Solution 3 - AndroidRavi BhojaniView Answer on Stackoverflow
Solution 4 - Androidthe100rabhView Answer on Stackoverflow
Solution 5 - AndroidAnkur GautamView Answer on Stackoverflow
Solution 6 - AndroidAmit PadekarView Answer on Stackoverflow
Solution 7 - AndroidFedorView Answer on Stackoverflow
Solution 8 - AndroidTal PressmanView Answer on Stackoverflow
Solution 9 - AndroiducMediaView Answer on Stackoverflow
Solution 10 - AndroidmasarapmabuhayView Answer on Stackoverflow
Solution 11 - AndroidRevakoOAView Answer on Stackoverflow