3

I am drawing a frame by frame animation to a canvas and I have about 100 pics that I'm using to do this (which is about 1.5MB total). I started out by just doing this:

s000 = BitmapFactory.decodeResource(getResources(), R.drawable.s0);
s001 = BitmapFactory.decodeResource(getResources(), R.drawable.s1); ...etc...

to every image and then drawing each image to the canvas:

c.drawBitmap(s000, X, Y, null);

to make an animation.

The problem is that I get this error "OutOfMemoryError: bitmap size exceeds VM budget". How would I load all the pics without getting this error? Is 1.5MB to much memory or do I have a memory leak? What would I do to fix the memory leak?

Thank you very much for your help. I am noob with android so could you please leave examples and not just tell me to do something that I wont understand :) Thanks again

alexward1230
  • 579
  • 3
  • 8
  • 25

3 Answers3

2

Try like this for every frame you draw:

s000 = BitmapFactory.decodeResource(getResources(), R.drawable.s0);
c.drawBitmap(s000, X, Y, null);
s000.recycle();
s000 = null;

This will try to release the memory after drawing the frame.

EDIT

myImgLen = 30;
Bitmap bitmap = null;
for (int i = 0; i < myImgLen; i++) {
    bitmap = BitmapFactory.decodeResource(getResources(), getResource‌​s().getIdentifier("s" + i, "drawable", getPackageName()));
    c.drawBitmap(bitmap, X, Y, null);
    bitmap.recycle();
    bitmap = null;
}
Caner
  • 57,267
  • 35
  • 174
  • 180
  • Thank you for your help, but now I have a new problem. My animation loops and I use the same Bitmaps over and over so if I recycle it then it gives me an error. At the beginning of my app I decode all the bitmaps like this: int myImgLen = 30; for (int i = 0; i < myImgLen; i++) myList.add(BitmapFactory.decodeResource(this.getResources(), this.getResources().getIdentifier("s" + i, "drawable", getPackageName()))); Do you know how I could decode each bitmap before drawing it instead of at the beginning? – alexward1230 Jul 05 '12 at 10:50
  • I cant believe something as simple as recycling the bitmap caused me so much trouble. Thank you very much for your help, it is working now!!! Yeah! On a side note it is working but since it has to reload the bitmap on every frame it draws its a little choppy/slow. Do you have any tips/pointers on how I can make it more smooth o wise one :) – alexward1230 Jul 05 '12 at 20:41
0

Store your image into res->drawable-xhdpi make folder inside res folder.
For more information see this link http://developer.android.com/guide/practices/screens_support.html.

Bhargav Panchal
  • 1,159
  • 1
  • 12
  • 27
  • They are stored there(or in hdpi), but to draw them to the canvas I have to do this; Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.s0); to every one of them and thats whats causing the problem I think. – alexward1230 Jul 05 '12 at 10:05
  • you have to make "drawable-xhdpi" folder inside res folder. Generally hdpi (high), and xhdpi (extra high). your image is 1.5 mb that's way you have to make drawable-xhdpi folder inside res folder – Bhargav Panchal Jul 05 '12 at 10:21
  • but my image(s) are a total of 1.5MB all together (and there are a lot of them) each image is 480x800 which belongs in the hdpi folder. I think... – alexward1230 Jul 05 '12 at 10:26
0

Maybe the sizes of the images are incorrect. Refer to this answer: Strange out of memory issue while loading an image to a Bitmap object.

Community
  • 1
  • 1