I am developing a game and I load 52 Bitmap files
using BitmapFactory
, and when running the App. I receive OutOfMemoryError
. Is that because the images in the drawable
folder are .JPG
and not .PNG
? Please let me know how to solve this issue.

- 1
- 27
- 104
- 226
2 Answers
JPG vs PNG doesn't matter, because when you load the bitmaps you uncompress them into raw data (basically the same as bmp). Uncompressed, each bitmap uses 4*width*height bytes. Depending on how big your images are, that can be very large. I would suggest using an LRUCache with a fixed size to hold only the images you actually need in memory and to kick out the unused ones.

- 90,003
- 9
- 87
- 127
I am dealing with 196 of png pictures in my app and ran into out of memory as well when I attempted to load all the images. My images are png, this means converting them from jpg to png didn't help.
I don't know if it's the optimum solution or not, but what I did is to start an IntentService from my main activity's OnCreate() method, first time it is called. Inside I check if a smaller version of the images is already stored on the internal storage. If the smaller versions are already created, I maintain a list of them and use them in my activities whenever I need to display them. If they are not present, which happens when the user installed the app for the first time, or the cache is cleared from the settings for this app, I create them by calling decodeSampledBitmapFromResource()
with the desired width and height. The desired width and height can be store in an xml file and be specific for different screens. The functions I use for resizing while maintaining the image aspect are provided below.
/**
* Calculate in sample size.
*
* @param options the options
* @param reqWidth the req width
* @param reqHeight the req height
* @return the int
*/
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;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
/**
* Decode sampled bitmap from resource.
*
* @param res the res
* @param resId the res id
* @param reqWidth the req width
* @param reqHeight the req height
* @return the bitmap
*/
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}

- 1,432
- 1
- 14
- 17