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.
This is the code from assignImages():
This is the error I am getting:
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
But when i change my assignImages method from above to include :
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
Here is the code within main that listens for a click and then calls another method to get the image.
Java:
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():
Java:
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:
Code:
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
Java:
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 :
Java:
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
Last edited: