I am developing a card game for Android. I have an OutOfMemoryError when I try to load some images. I read a lot of answers about this common and common problem in android, but they all deal when the image to load is very large. I control the rotation of the screen myself, so I do not restart the new activity during rotation. I already read the following questions:
Memory exception due to large raster image size
Android: Gallery Exception
Android image processing troubleshooting
One thing that I do not understand is that if the sum of all my images (png files) in the res folder is about 800 kb. But in Logcat, I notice that the size when creating a game (loading card images) goes faster than 20 mb. I use: this code to upload images
Resources r = this.getContext().getResources(); CardView.loadCardImages(CardView.EAlone.CARDS, 1027, 615, r.getDrawable(R.drawable.cards)); CardView.loadCardImages(CardView.EAlone.GREEN, mCardWidth, mCardHeight, r.getDrawable(R.drawable.green)); CardView.loadCardImages(CardView.EAlone.RED, mCardWidth, mCardHeight, r.getDrawable(R.drawable.red)); CardView.loadCardImages(CardView.EAlone.WHITE, mCardWidth, mCardHeight, r.getDrawable(R.drawable.white));
CardView.LoadCardImages:
public static void loadCardImages(EAlone mode, int widthPixels, int heightPixels, Drawable tile) { Canvas canvas; switch (mode) { case GREEN: mAloneGreen = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888); canvas = new Canvas(mAloneGreen); break; case RED: mAloneRed = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888); canvas = new Canvas(mAloneRed); break; case WHITE: mAloneWhite = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888); canvas = new Canvas(mAloneWhite); break; default: mcardImages = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888); canvas = new Canvas(mcardImages); } tile.setBounds(0, 0, widthPixels, heightPixels); tile.draw(canvas); tile.setCallback(null); }
mCardimages contains all facets of maps. Then I use this file to create single maps, trimming the necessary map from the file. After these loads, I load some other transparencies in the parent view.
dealerSign = new ImageView(getContext()); dealerSign.setImageResource(R.drawable.dealer); dealerSign.setAdjustViewBounds(true); dealerSign.setLayoutParams(new Gallery.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
I get an error in the setImageResource line.
04-09 22:04:37.226: E/AndroidRuntime(722): FATAL EXCEPTION: main 04-09 22:04:37.226: E/AndroidRuntime(722): java.lang.OutOfMemoryError 04-09 22:04:37.226: E/AndroidRuntime(722): at android.graphics.Bitmap.nativeCreate(Native Method) 04-09 22:04:37.226: E/AndroidRuntime(722): at android.graphics.Bitmap.createBitmap(Bitmap.java:605) 04-09 22:04:37.226: E/AndroidRuntime(722): at android.graphics.Bitmap.createBitmap(Bitmap.java:551) 04-09 22:04:37.226: E/AndroidRuntime(722): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437) 04-09 22:04:37.226: E/AndroidRuntime(722): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:524) 04-09 22:04:37.226: E/AndroidRuntime(722): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499) 04-09 22:04:37.226: E/AndroidRuntime(722): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351) 04-09 22:04:37.226: E/AndroidRuntime(722): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:767) 04-09 22:04:37.226: E/AndroidRuntime(722): at android.content.res.Resources.loadDrawable(Resources.java:1937) 04-09 22:04:37.226: E/AndroidRuntime(722): at android.content.res.Resources.getDrawable(Resources.java:664) 04-09 22:04:37.226: E/AndroidRuntime(722): at android.widget.ImageView.resolveUri(ImageView.java:537) 04-09 22:04:37.226: E/AndroidRuntime(722): at android.widget.ImageView.setImageResource(ImageView.java:310) 04-09 22:04:37.226: E/AndroidRuntime(722): at game.spathi.GameView.init(GameView.java:261) 04-09 22:04:37.226: E/AndroidRuntime(722): at game.spathi.Game.onCreate(Game.java:92) ...
What am I doing wrong here?
Thanks!