First off, I have read many posts and articles about out of memory exceptions but none of them have helped with my situation. What I'm trying to do is load an image from the sd card but scale it to an exact pixel size.
I first get the width and height of the image and calculate the sample size:
final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(backgroundPath, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, getWidth(), getHeight()); Here's how I get the sample size (although its not really relevant):
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; // NOTE: we could use Math.floor here for potential better image quality // however, this also results in more out of memory issues if (height > reqHeight || width > reqWidth) { if (width > height) { inSampleSize = Math.round((float)height / (float)reqHeight); } else { inSampleSize = Math.round((float)width / (float)reqWidth); } } return inSampleSize; } Now that I have a sample size I load the image from disk to an approximate size (sample size):
// Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; options.inPurgeable = true; Bitmap bmp = BitmapFactory.decodeFile(backgroundPath, options); Now, I scale this bitmap that I have created to the exact size I need and clean up:
// scale the bitmap to the exact size we need Bitmap editedBmp = Bitmap.createScaledBitmap(bmp, (int) (width * scaleFactor), (int) (height * scaleFactor), true); // clean up first bitmap bmp.recycle(); bmp = null; System.gc(); // I know you shouldnt do this, but I'm desperate The above step is usually get my out of memory exception. Does anyone know a way to load an exact size bitmap from disk to avoid having to create two separate bitmaps like above?
Also, it seems like more exceptions occur when the user runs this code for a second time (sets a new image). However, I make sure to unload the drawable that was created from the bitmap which allows it to be garbage collected before this code is run again.
Any suggestions?
Thanks, Nick
editedBmp, log the width and height of it, as well as the scaleFactor to make sure that your parameters are correct.