0

I'm developing a game for little children, which contains different educative exercises. In one of them I need a picture background, so I'm loading it and scale it with Matrix to fill screen. But sometimes loading picture causes crash.

public DrawThread(SurfaceHolder surfaceHolder, Resources resources, float screen_x_max, float screen_y_max){
    this.surfaceHolder = surfaceHolder;
    screenWidth=screen_x_max;
    screenHeight=screen_y_max;
    // picture for bg
    picture = BitmapFactory.decodeResource(resources, R.drawable.background);
    ...
}

At the end of this thread I recycle it and null.

if (this.picture!=null)
    {
        this.picture.recycle();
        this.picture=null;
    }

But app still crases. There is error log:

java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:494)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:370)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:393)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:419)
at lexu.me.childstudy_lite.DrawThreadFindAnimals2.<init>(findAnimals2.java:271)
at lexu.me.childstudy_lite.findanimals2_view.surfaceCreated(findAnimals2.java:186)
at android.view.SurfaceView.updateWindow(SurfaceView.java:548)
at android.view.SurfaceView.dispatchDraw(SurfaceView.java:353)
at android.view.ViewGroup.drawChild(ViewGroup.java:1737)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466)
at android.view.ViewGroup.drawChild(ViewGroup.java:1737)

Thanks.

LEX
  • 634
  • 1
  • 7
  • 11
  • You're trying to load a very large bitmap and downscale it with a matrix? The very large bitmap will use a very large amount of memory when decoded. Start with appropriately scaled bitmaps, using the various drawable folders (e.g. drawable-hdpi) to load images with a proper initial scale. – CSmith Aug 20 '12 at 13:32
  • yes, it quite large: 1200*800 (344 kB). – LEX Aug 20 '12 at 13:45

3 Answers3

0

This is a classic problem on Android. This should provide you with plenty of tips and approaches :

Lazy load of images in ListView

Community
  • 1
  • 1
Slickelito
  • 1,786
  • 20
  • 28
0

By down sampling bitmap, you can avoid OME. Have a look at following link. Hope it helps.

Android:Issue Image resolution

Edit: In that link, its taking images from Asset folder. You can modify it according to your need.

Community
  • 1
  • 1
Braj
  • 2,164
  • 2
  • 26
  • 44
0

You are getting an Out Of Memory Exception. To avoid this, have a look at this link: android how to handle out of memory exception

There is a ton of information on the web that will help you with this problem.

I hope this helps.

Community
  • 1
  • 1
Luke Taylor
  • 9,481
  • 13
  • 41
  • 73