9 Replies - 699 Views - Last Post: 09 June 2017 - 06:25 AM

#1 robrod14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-June 17

Getting OutOfMemoryError when dealing with images

Posted 07 June 2017 - 09:47 AM

I am new to Android Studio and this is my first attempt at making an app. I'm making a card game, and the app initially loads fine. As i tap the deck a card is delt. If i tap the deck again a new card is delt and placed on top of the card shown before.

Here is the code within main that listens for a click and then calls another method to get the image.

player1Deck.setonclickListener(new View.onclickListener(){

            public void onclick(View view){
                int deal = 0;
                PlayingCards c = null;
                String name;

                deal = getRandom();
                c = b.get(deal);
                name = c.getName2();
                pc.assignImages(name, player1Empty);

                //flip player 1 cards
                /*pc.assignImages(pc.cards.get(countPlayer1), player1Empty);*/

                player1Empty.setVisibility(View.VISIBLE);
                countPlayer1++;
            }
        });


This is the code from assignImages():
public void assignImages (String card, ImageView image) {
        switch(card){
            //Clubs
            case "clubs2":
                image.setImageResource(R.drawable.c2clubs);
                break;
            case "clubs3":
                image.setImageResource(R.drawable.c3clubs);
                break;
            case "clubs4":
                image.setImageResource(R.drawable.c4clubs);
                break;
            case "clubs5":
                image.setImageResource(R.drawable.c5clubs);
                break;
            case "clubs6":
                image.setImageResource(R.drawable.c6clubs);
                break;
            case "clubs7":
                image.setImageResource(R.drawable.c7clubs);
                break;
            case "clubs8":
                image.setImageResource(R.drawable.c8clubs);
                break;
            case "clubs9":
                image.setImageResource(R.drawable.c9clubs);
                break;
            case "clubs10":
                image.setImageResource(R.drawable.c10clubs);
                break;
            case "clubs11":
                image.setImageResource(R.drawable.jackclubs);
                break;
            case "clubs12":
                image.setImageResource(R.drawable.queenclubs);
                break;
            case "clubs13":
                image.setImageResource(R.drawable.kingclubs);
                break;
            case "clubs14":
                image.setImageResource(R.drawable.aceclub);
                break;
          }
}



This is the error I am getting:

java.lang.OutOfMemoryError: Failed to allocate a 245000012 byte allocation with 16777216 free bytes and 196MB until OOM
                                                                                    at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                                                                                    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                                    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:700)
                                                                                    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:535)
                                                                                    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1179)
                                                                                    at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:770)
                                                                                    at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:621)
                                                                                    at android.content.res.Resources.getDrawable(Resources.java:1642)
                                                                                    at android.content.Context.getDrawable(Context.java:525)
                                                                                    at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:30)
                                                                                    at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:372)
                                                                                    at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:202)
                                                                                    at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:190)
                                                                                    at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
                                                                                    at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:73)
                                                                                    at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:81)
                                                                                    at com.example.robertrodriquez.spit.PlayingCards.assignImages(PlayingCards.java:173)
                                                                                    at com.example.robertrodriquez.spit.MainActivity$1.onclick(MainActivity.java:67)
                                                                                    at android.view.View.performClick(View.java:6261)
                                                                                    at android.view.View$PerformClick.run(View.java:23748)
                                                                                    at android.os.Handler.handleCallback(Handler.java:751)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                    at android.os.Looper.loop(Looper.java:154)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)




I have searched the internet and they talk about using bitmap and trying to show preview of the images instead of the full image. I figured this would resolve my issue, I was trying to use the following code

private static Bitmap decodeResource(Resources res, int id) {
        Bitmap bitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        for (options.inSampleSize = 1; options.inSampleSize <= 32; options.inSampleSize++) {
            try {
                bitmap = BitmapFactory.decodeResource(res, id, options);
                Log.d("hereiam", "Decoded successfully for sampleSize " + options.inSampleSize);
                break;
            } catch (OutOfMemoryError outOfMemoryError) {
                // If an OutOfMemoryError occurred, we continue with for loop and next inSampleSize value
                Log.e("heriam", "outOfMemoryError while reading file for sampleSize " + options.inSampleSize
                        + " retrying with higher value");
            }
        }
        return bitmap;


But when i change my assignImages method from above to include :

public void assignImages (String card, ImageView image) {
        switch(card){
            //Clubs
            case "clubs1":
                image.setImageBitmap(decodeResource(getResources(), R.drawable.c2clubs));
            


I can't get "getResources()" to resolve. It looks like i have to create a getResources() method but i'm not sure what to put inside this method. I've searched and found out the getResources should return a resource which makes sense but i'm still not sure of the code that should go in this method. The decodeResource() method already has the id of the image i want with "R.drawable.c2clubs".

If someone could help me figure out what i'm missing it would be appreciated.

Thank You

Is This A Good Question/Topic? 0
  • +

Replies To: Getting OutOfMemoryError when dealing with images

#2 wseng92  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 299
  • Joined: 23-September 15

Re: Getting OutOfMemoryError when dealing with images

Posted 07 June 2017 - 10:00 AM

Are your class extends to AppCompatActivity or Fragment?

For AppCompatActivity, try
getApplicationContext().getResource();

For Fragment, try
 getActivity().getResource();

This post has been edited by wseng92: 07 June 2017 - 10:03 AM

Was This Post Helpful? 0
  • +
  • -

#3 robrod14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-June 17

Re: Getting OutOfMemoryError when dealing with images

Posted 07 June 2017 - 10:03 AM

View Postwseng92, on 07 June 2017 - 10:00 AM, said:

Are your class extends to AppCompatActivity or Fragment?
For AppCompatActivity, try getApplicationContext().getResource();
For Fragment, try Try getActivity().getResource();



No my class extends neither... I will try the each one to see if i get a diff result.

Thank you
Was This Post Helpful? 0
  • +
  • -

#4 robrod14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-June 17

Re: Getting OutOfMemoryError when dealing with images

Posted 07 June 2017 - 10:45 AM

Ok, I tried both options and this is what i got


For AppCompatActivity:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
                                                                                      at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:106)
                                                                                      at com.example.robertrodriquez.spit.PlayingCards.assignImages(PlayingCards.java:407)
                                                                                      at com.example.robertrodriquez.spit.MainActivity$1.onclick(MainActivity.java:67)
                                                                                      at android.view.View.performClick(View.java:6261)
                                                                                      at android.view.View$PerformClick.run(View.java:23748)
                                                                                      at android.os.Handler.handleCallback(Handler.java:751)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:154)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)



For fragment:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.view.ContextThemeWrapper.getResources()' on a null object reference
                                                                                      at com.example.robertrodriquez.spit.PlayingCards.assignImages(PlayingCards.java:352)
                                                                                      at com.example.robertrodriquez.spit.MainActivity$1.onclick(MainActivity.java:67)
                                                                                      at android.view.View.performClick(View.java:6261)
                                                                                      at android.view.View$PerformClick.run(View.java:23748)
                                                                                      at android.os.Handler.handleCallback(Handler.java:751)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:154)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)


Was This Post Helpful? 0
  • +
  • -

#5 wseng92  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 299
  • Joined: 23-September 15

Re: Getting OutOfMemoryError when dealing with images

Posted 07 June 2017 - 06:39 PM

Post your class
Was This Post Helpful? 0
  • +
  • -

#6 robrod14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-June 17

Re: Getting OutOfMemoryError when dealing with images

Posted 07 June 2017 - 07:09 PM

first class (main)
package com.example.robertrodriquez.spit;

import android.support.v4.content.res.TypedArrayUtils;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;



//import android.widget.TextView;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    ImageView player1Deck, player2Deck, player1Empty, player2Empty;

    int countPlayer1 = 0;
    int[] drawCard;
    PlayingCards pc = new PlayingCards();
    int[] deck;

    ArrayList<PlayingCards> a = new ArrayList<>();
    ArrayList<PlayingCards> b = new ArrayList<PlayingCards>();
    ArrayList<Integer> cardsCalled = new ArrayList<>();

    int randHigh = 52;
    int randLow = 0;
    int deckIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    // Example of a call to a native method
    //TextView tv = (TextView) findViewById(R.id.sample_text);
    //tv.setText(stringFromJNI());

        player1Deck = (ImageView) findViewById(R.id.player1Deck);
        player2Deck = (ImageView) findViewById(R.id.player2Deck);
        player1Empty = (ImageView) findViewById(R.id.player1Empty);
        player2Empty = (ImageView) findViewById(R.id.player2Empty);

        player1Empty.setVisibility(View.VISIBLE);
        player2Empty.setVisibility(View.VISIBLE);

        b = deck();


        player1Deck.setonclickListener(new View.onclickListener(){

            public void onclick(View view){
                int deal = 0;
                PlayingCards c = null;
                String name;

                deal = getRandom();
                c = b.get(deal);
                name = c.getName2();
                pc.assignImages(name, player1Empty);

                //flip player 1 cards
                /*pc.assignImages(pc.cards.get(countPlayer1), player1Empty);*/

                player1Empty.setVisibility(View.VISIBLE);
                countPlayer1++;
            }
        });



        //player2Deck.setonclickListener(new View.onclickListener());

    }




    public ArrayList<PlayingCards> deck(){

        for(int n=2; n < 15; n++) {
            PlayingCards c = new PlayingCards("clubs" + n, "clubs", n);
            a.add(c);
        }

        for(int n=2; n < 15; n++) {
            PlayingCards c = new PlayingCards("diamonds" + n, "diamonds", n);
            a.add(c);
        }

        for(int n=2; n < 15; n++) {
            PlayingCards c = new PlayingCards("hearts" + n, "hearts", n);
            a.add(c);
        }

        for(int n=2; n < 15; n++) {
            PlayingCards c = new PlayingCards("spades" + n, "spades", n);
            a.add(c);
        }

        return a;
    }


    public int getRandom(){

        Random r = new Random();
        deckIndex = r.nextInt(randHigh-randLow) + randLow;

        if (cardsCalled.contains(deckIndex)) {
            getRandom();
        } else {
            cardsCalled.add(deckIndex);
        }

        return deckIndex;
    }


    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }
}




Second class (Playingcards)
package com.example.robertrodriquez.spit;

import android.content.res.Resources;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.Random;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import static android.support.v4.content.res.ResourcesCompat.getDrawable;

/**
 * Created by robertrodriquez on 6/5/17.
 */

public class PlayingCards extends Fragment {

    int suitLow = 1;
    int suitHigh = 5;
    int cardLow = 2;
    int cardHigh = 15;
    int suit;
    int card;


    private String suit2;
    private int number2;
    private String name2;


    public PlayingCards(){

    }

    public PlayingCards(String name2, String suit2, int number2){

        this.name2 = name2;
        this.suit2 = suit2;
        this.number2 = number2;

    }


    public String getSuit2(){

        return this.suit2;
    }

    public int getNumber2(){

        return this.number2;
    }


    public String getName2(){

        return this.name2;
    }


    /*ArrayList<PlayingCards> cards = new ArrayList();*/

    public int[] createCard() {

        //creating a random suit
        Random r = new Random();
        suit = r.nextInt(suitHigh - suitLow) + suitLow;

        //creating a random card
        Random r2 = new Random();
        card = r2.nextInt(cardHigh - cardLow) + cardLow;
        return new int[] {suit, card};
    }


    /*public void assignImages (String card, ImageView image) {
        switch(card){
            //Clubs
            case "clubs2":
                image.setImageResource(R.drawable.c2clubs);
                break;
            case "clubs3":
                image.setImageResource(R.drawable.c3clubs);
                break;
            case "clubs4":
                image.setImageResource(R.drawable.c4clubs);
                break;
            case "clubs5":
                image.setImageResource(R.drawable.c5clubs);
                break;
            case "clubs6":
                image.setImageResource(R.drawable.c6clubs);
                break;
            case "clubs7":
                image.setImageResource(R.drawable.c7clubs);
                break;
            case "clubs8":
                image.setImageResource(R.drawable.c8clubs);
                break;
            case "clubs9":
                image.setImageResource(R.drawable.c9clubs);
                break;
            case "clubs10":
                image.setImageResource(R.drawable.c10clubs);
                break;
            case "clubs11":
                image.setImageResource(R.drawable.jackclubs);
                break;
            case "clubs12":
                image.setImageResource(R.drawable.queenclubs);
                break;
            case "clubs13":
                image.setImageResource(R.drawable.kingclubs);
                break;
            case "clubs14":
                image.setImageResource(R.drawable.aceclub);
                break;

            //Diamonds
            case "diamonds2":
                image.setImageResource(R.drawable.c2diamond);
                break;
            case "diamonds3":
                image.setImageResource(R.drawable.c3diamond);
                break;
            case "diamonds4":
                image.setImageResource(R.drawable.c4diamond);
                break;
            case "diamonds5":
                image.setImageResource(R.drawable.c5diamond);
                break;
            case "diamonds6":
                image.setImageResource(R.drawable.c6diamond);
                break;
            case "diamonds7":
                image.setImageResource(R.drawable.c7diamond);
                break;
            case "diamonds8":
                image.setImageResource(R.drawable.c8diamond);
                break;
            case "diamonds9":
                image.setImageResource(R.drawable.c9diamond);
                break;
            case "diamonds10":
                image.setImageResource(R.drawable.c10diamond);
                break;
            case "diamonds11":
                image.setImageResource(R.drawable.jackdiamonds);
                break;
            case "diamonds12":
                image.setImageResource(R.drawable.queendiamonds);
                break;
            case "diamonds13":
                image.setImageResource(R.drawable.kingdiamonds);
                break;
            case "diamonds14":
                image.setImageResource(R.drawable.acediamond);
                break;

            //Hearts
            case "hearts2":
                image.setImageResource(R.drawable.c2hearts);
                break;
            case "hearts3":
                image.setImageResource(R.drawable.c3hearts);
                break;
            case "hearts4":
                image.setImageResource(R.drawable.c4hearts);
                break;
            case "hearts5":
                image.setImageResource(R.drawable.c5hearts);
                break;
            case "hearts6":
                image.setImageResource(R.drawable.c6hearts);
                break;
            case "hearts7":
                image.setImageResource(R.drawable.c7hearts);
                break;
            case "hearts8":
                image.setImageResource(R.drawable.c8hearts);
                break;
            case "hearts9":
                image.setImageResource(R.drawable.c9hearts);
                break;
            case "hearts10":
                image.setImageResource(R.drawable.c10hearts);
                break;
            case "hearts11":
                image.setImageResource(R.drawable.jackhearts);
                break;
            case "hearts12":
                image.setImageResource(R.drawable.queenhearts);
                break;
            case "hearts13":
                image.setImageResource(R.drawable.kinghearts);
                break;
            case "hearts14":
                image.setImageResource(R.drawable.acehearts);
                break;

            //Spades
            case "spades2":
                image.setImageResource(R.drawable.c2spade);
                break;
            case "spades3":
                image.setImageResource(R.drawable.c3spade);
                break;
            case "spades4":
                image.setImageResource(R.drawable.c4spade);
                break;
            case "spades5":
                image.setImageResource(R.drawable.c5spade);
                break;
            case "spades6":
                image.setImageResource(R.drawable.c6spade);
                break;
            case "spades7":
                image.setImageResource(R.drawable.c7spade);
                break;
            case "spades8":
                image.setImageResource(R.drawable.c8spade);
                break;
            case "spades9":
                image.setImageResource(R.drawable.c9spade);
                break;
            case "spades10":
                image.setImageResource(R.drawable.c10spade);
                break;
            case "spades11":
                image.setImageResource(R.drawable.jackspade);
                break;
            case "spades12":
                image.setImageResource(R.drawable.queenspade);
                break;
            case "spades13":
                image.setImageResource(R.drawable.kingspades);
                break;
            case "spades14":
                image.setImageResource(R.drawable.acespade);
                break;
        }
    }*/



    public void assignImages (String card, ImageView image) {
        switch(card){
            //Clubs

            case "clubs2":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c2clubs));
                break;
            case "clubs3":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c3clubs));
                break;
            case "clubs4":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c4clubs));
                break;
            case "clubs5":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c5clubs));
                break;
            case "clubs6":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c6clubs));
                break;
            case "clubs7":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c7clubs));
                break;
            case "clubs8":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c8clubs));
                break;
            case "clubs9":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c9clubs));
                break;
            case "clubs10":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c10clubs));
                break;
            case "clubs11":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.jackclubs));
                break;
            case "clubs12":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.queenclubs));
                break;
            case "clubs13":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.kingclubs));
                break;
            case "clubs14":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.aceclub));
                break;

            //Diamonds
            case "diamonds2":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c2diamond));
                break;
            case "diamonds3":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c3diamond));
                break;
            case "diamonds4":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c4diamond));
                break;
            case "diamonds5":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c5diamond));
                break;
            case "diamonds6":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c6diamond));
                break;
            case "diamonds7":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c7diamond));
                break;
            case "diamonds8":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c8diamond));
                break;
            case "diamonds9":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c9diamond));
                break;
            case "diamonds10":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c10diamond));
                break;
            case "diamonds11":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.jackdiamonds));
                break;
            case "diamonds12":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.queendiamonds));
                break;
            case "diamonds13":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.kingdiamonds));
                break;
            case "diamonds14":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.acediamond));
                break;

            //Hearts
            case "hearts2":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c2hearts));
                break;
            case "hearts3":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c3hearts));
                break;
            case "hearts4":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c4hearts));
                break;
            case "hearts5":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c5hearts));
                break;
            case "hearts6":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c6hearts));
                break;
            case "hearts7":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c7hearts));
                break;
            case "hearts8":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c8hearts));
                break;
            case "hearts9":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c9hearts));
                break;
            case "hearts10":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c10hearts));
                break;
            case "hearts11":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.jackhearts));
                break;
            case "hearts12":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.queenhearts));
                break;
            case "hearts13":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.kinghearts));
                break;
            case "hearts14":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.acehearts));
                break;

            //Spades
            case "spades2":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c2spade));
                break;
            case "spades3":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c3spade));
                break;
            case "spades4":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c4spade));
                break;
            case "spades5":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c5spade));
                break;
            case "spades6":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c6spade));
                break;
            case "spades7":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c7spade));
                break;
            case "spades8":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c8spade));
                break;
            case "spades9":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c9spade));
                break;
            case "spades10":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.c10spade));
                break;
            case "spades11":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.jackspade));
                break;
            case "spades12":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.queenspade));
                break;
            case "spades13":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.kingspades));
                break;
            case "spades14":
                image.setImageBitmap(decodeResource(getActivity().getResources(), R.drawable.acespade));
                break;
        }
    }


    private static Bitmap decodeResource(Resources res, int id) {
        Bitmap bitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        for (options.inSampleSize = 1; options.inSampleSize <= 32; options.inSampleSize++) {
            try {
                bitmap = BitmapFactory.decodeResource(res, id, options);
                Log.d("hereiam", "Decoded successfully for sampleSize " + options.inSampleSize);
                break;
            } catch (OutOfMemoryError outOfMemoryError) {
                // If an OutOfMemoryError occurred, we continue with for loop and next inSampleSize value
                Log.e("heriam", "outOfMemoryError while reading file for sampleSize " + options.inSampleSize
                        + " retrying with higher value");
            }
        }
        return bitmap;
    }


}



Was This Post Helpful? 0
  • +
  • -

#7 wseng92  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 299
  • Joined: 23-September 15

Re: Getting OutOfMemoryError when dealing with images

Posted 07 June 2017 - 07:24 PM

So you want to extends Fragment Class to your second class?

You need to have below code in order to create Fragment

public class SecondClass extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle 
				savedInstanceState) {

        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.toolbar_fragment,
                container, false);
        return view;
    }
}

Follow this example to create fragment
Using Fragments in Android Studio - An Example

This post has been edited by wseng92: 07 June 2017 - 07:25 PM

Was This Post Helpful? 0
  • +
  • -

#8 robrod14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-June 17

Re: Getting OutOfMemoryError when dealing with images

Posted 08 June 2017 - 07:24 AM

ok so i added your exact method at the bottom of my second class. I then imported all the modules that needed. I have two questions.

1. i don't call this new method onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) anywhere in my code. Since i added this method am i supposed to call it from somewhere?

2. in your new method you told me to add the only thing that didn't resolve after importing modules was "toolbar_fragment" in R.layout.toolbar_fragment. When i select the red lightbulb for hints my options are "Create layout resource file 'toolbar_fragment.xml', Create layout value resource 'toolbar_fragment', Create field 'toolbar_fragment', rename reference"
which should i do to resolve this error?

Thanks again
Was This Post Helpful? 0
  • +
  • -

#9 wseng92  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 299
  • Joined: 23-September 15

Re: Getting OutOfMemoryError when dealing with images

Posted 08 June 2017 - 09:21 AM

Forget about the Fragment...

Have you tried adding
android:largeHeap="true"
to your Mainfest ?

Others alternatives Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM

This post has been edited by wseng92: 08 June 2017 - 09:23 AM

Was This Post Helpful? 0
  • +
  • -

#10 robrod14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-June 17

Re: Getting OutOfMemoryError when dealing with images

Posted 09 June 2017 - 06:25 AM

sorry about the delayed response. I removed the fragment code. So i'm back to my original code where sometimes i can click on the deck and get a new card but then sometimes it will crash saying out of memory. The random times i am able to get a new card i can't ever get more than 7 cards before it crashes saying out of memory.

I tried your suggestion with the manifest and that didn't change the results. I then went to my resources folder and resized all my images to 4kb thinking that would fix the issue, but i still get the error.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1