1

I am working on game, where player will fly in sky and player will avoid many enemies. But on SGS3 I have "Out of memory 720016 - byte allocation. I have so many bitmaps and i use this code:

  BitmapFactory.Options bfOptions=new BitmapFactory.Options();
        bfOptions.inSampleSize = 1;
        bfOptions.inDither=false;                     
        bfOptions.inPurgeable=true;
        bfOptions.inInputShareable=true;
        bfOptions.inTempStorage=new byte[16 * 1024]; 

and I decode many bitmaps with this code

amunicja2_bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.bron2take,bfOptions);
amunicja2_bitmap_full =  Bitmap.createScaledBitmap(amunicja2_bitmap, width_amunicja2,  height_amunicja2, false);

I have 30 bitmaps like that. My game is still small (1,25MB but there is a OUT OF MEMORY ERROR) What I can do?

kubaork
  • 161
  • 2
  • 12

2 Answers2

0

You can see my answer here.It will be useful to handle your bitmap efficiently.

How to make application more responsive which uses several bitmaps?

Community
  • 1
  • 1
Kailash Dabhi
  • 3,473
  • 1
  • 29
  • 47
0

Its a known bug, its not because of large files.

Bitmap is stored in native heap, but it will get garbage collected automatically, calling recycle() or by other process by making weakreference or softreference To fix OutOfMemory you should do something like this:

options.inSampleSize = calculateInSampleSize(options,bitmapWidth,bitmapHeight);

public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {

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;
}

For example, an image with resolution 2048x1536 that is decoded with an inSampleSize of 4 produces a bitmap of approximately 512x384

loading images efficiently

Santhosh
  • 1,867
  • 2
  • 16
  • 23
  • I think that you didn't noticed, that I alredy use "inSampleSize" amunicja2_bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.bron2take,bfOptions); ((bfOptions = BitmapFactory.Options();)) – kubaork Dec 28 '13 at 12:39
  • Yes but you set it to 1 (the default) so it doesn't downscale. – Reuben Scratton Dec 28 '13 at 12:47