How to fix a 鈥渏ava.lang.OutOf MemoryError: bitmap size exceeds VM budget 鈥?error on andr oid

by Sheepz » Tue, 31 Aug 2010 04:37:17 GMT


Sponsored Links
 i all, I've seen this question asked many times but none of the
answers really made sense in what I'm doing... I have an android game
(see source at Google source control:
http://code.google.com/p/bestcardgameever-android/source/browse/#svn/trunk/bestcardgameever-android/Branches/Yaniv%20For%20Android)
It's a card game, and I've used a method that redraws each hand every
round - that might be wasteful but I couldn't think of a better way to
do it. Here is the code for the redrawHand method:

private void redrawHand(Hand hand) {
ImageView[] cardView = hand.getCardsViews();
View container = hand.getContainer();
for (int i = 0; i < GameData.YANIV_NUM_CARDS; i++) {
PlayingCard card = hand.getCardByLocation(i);
if (card != null) {
// Show Card
cardView[i].setVisibility(View.VISIBLE);
int resId;
if (hand.shouldCardsBeShown()) {
resId = card.getImageResourceId();
} else {
resId = R.drawable.back;
}
cardView[i].setImageResource(resId);
if (hand.isHumanPlayer()) {
// when selected, move up 15 pixels
boolean isSelected = hand.isCardSelected(i);
((LinearLayout.LayoutParams)
cardView[i].getLayoutParams()).bottomMargin = isSelected? 15 : 0;
} } else {
cardView[i].setVisibility(View.INVISIBLE); } } // Set player name
hand.getHandLabelView().setText(hand.getHandLabel());
container.requestLayout(); }

After installing ACRA (http://code.google.com/p/acra/wiki/ACRAHowTo) I
have started getting crash reports from devices saying the following:

java.lang.OutOfMemoryError: bitmap size exceeds VM budget at
android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:363) at
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:212) at
android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:
673) at android.content.res.Resources.loadDrawable(Resources.java:
1639) at android.content.res.Resources.getDrawable(Resources.java:535)
at android.widget.ImageView.resolveUri(ImageView.java:541) at
android.widget.ImageView.setImageResource(ImageView.java:293) at
com.geekadoo.ui.Yaniv.redrawHand(Yaniv.java:765) at
com.geekadoo.ui.Yaniv.performYaniv(Yaniv.java:539) at
com.geekadoo.ui.Yaniv.performYanivHandler(Yaniv.java:503) at
com.geekadoo.ui.Yaniv.access$1(Yaniv.java:502) at com.geekadoo.ui.Yaniv
$2.onClick(Yaniv.java:323) at android.view.View.performClick(View.java:
2196) at android.view.View.onTouchEvent(View.java:3849) at
android.widget.TextView.onTouchEvent(TextView.java:6376) at
android.view.View.dispatchTouchEvent(View.java:3385) at
android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:872) at
android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:872) at
android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:872) at
android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:872) at
android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:872) at
android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:872) at
android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:872) at
android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:872) at
com.android.internal.policy.impl.PhoneWindow
$DecorView.superDispatchTouchEvent(PhoneWindow.java:1764) at
com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:
1213) at android.app.Activity.dispatchTouchEvent(Activity.java:2066)
at com.android.internal.policy.impl.PhoneWindo



Other Threads

1. How to create Multiple instances of an Activity From a Service?

I was wondering is it possible to create multiple instances of a
single Activity in Android?

I currently start my own inCall screen for a Voip Test by using the
following code: (Used in a Service)

---

             public void initInCallScreen(String pName, String phoneNumber,
int contactID, boolean
                        callDirection, int lineID){

                //starts in callScreen dialog
        final Intent myIntent = new Intent(context,
CallDialogActivity.class);
        myIntent.putExtra("NAME", pName);
        myIntent.putExtra("NUMBER", phoneNumber);
        myIntent.putExtra("ID", contactID);
        myIntent.putExtra("CALLTYPE", callDirection); //True =
Incoming, False = Outgoing
        myIntent.putExtra("LINEID", lineID);
        myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(myIntent);

---

This allows me to start the Activity fine.

However when I call it for a second it just returns to the Activity
already created rather than creating a new Activity and placing it on
the stack.

I would like to be able to create the activity multiple times so that
I have two or 3 Activities on the stack and the user can switch
between them, using Home, Back buttons etc...

Is this possible and if so what am I doing wrong?

-- 

2. Launch the WebBrowser with an intent (containing url) and then close it automatically?





I guess you can do whatever it is task killer apps do to kill a specific
task / process, but that's probably a massive hack.

Better option, I'd think, would be to control this by showing a WebView
instead of launching the browser, with which you can do whatever you want.

-------------------------------------------------------------------------------------------------
TreKing - Chicago transit tracking app for Android-powered devices
http://sites.google.com/site/rezmobileapps/treking

-- 

3. TO CHANGE YOUR LIFE BY A SECOND :

4. onclick open new view

5. Emulator keeps slowing down after a couple of deployments/Run as Android Application in Eclipse

6. Android Application design?

7. Official IMDB App!