0

I've written this switch statement in android studio however whenever the emulator is run it crashes as it says that the device has run out of memory. Each image is only 20kb but I think I need to increase the 'heap' size or whatever its called? Sorry i'm new to android. I will include the switch statement and error message I am displayed in debugger below. Thank for any help in advance :)

Java Switch Statement

    rating = 5;

    switch(rating){
    case 0: 
            ratingBar.setImageResource(R.drawable.star0);
            break;

    case 1: 
            ratingBar.setImageResource(R.drawable.star05);
            break;

    case 2:
            ratingBar.setImageResource(R.drawable.star1);
            break;

    case 3:
            ratingBar.setImageResource(R.drawable.star15);
            break;
    case 4:
            ratingBar.setImageResource(R.drawable.star2);
            break;
    case 5:
            ratingBar.setImageResource(R.drawable.star25);
            break;
    case 6:
            ratingBar.setImageResource(R.drawable.star3);
            break;
    case 7:
            ratingBar.setImageResource(R.drawable.star35);
            break;
    case 8:
            ratingBar.setImageResource(R.drawable.star4);
            break;
    case 9:
            ratingBar.setImageResource(R.drawable.star45);
            break;
    case 10:
            ratingBar.setImageResource(R.drawable.star5);
            break;
    }

Debugger Error Message

1504-1504/com.cs13ppz.virtualgallery E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.cs13ppz.virtualgallery, PID: 1504
    java.lang.OutOfMemoryError
            at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:594)
            at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:429)
            at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
            at android.content.res.Resources.loadDrawable(Resources.java:2110)
            at android.content.res.Resources.getDrawable(Resources.java:700)
            at android.widget.ImageView.resolveUri(ImageView.java:638)
            at android.widget.ImageView.setImageResource(ImageView.java:367)
            at com.cs13ppz.virtualgallery.ArtworkLibrary.onCreate(ArtworkLibrary.java:41)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
trincot
  • 317,000
  • 35
  • 244
  • 286
Phillip
  • 59
  • 3
  • 9

2 Answers2

1

You can try putting the following to application tag in your project's manifest file..

android:largeHeap="true"

But the problem seems to be with your emulator, try changing it's settings or test your app on an actual device.

Mark as right if it works for you.

P.S Always use optimized images. and do refer the following link
Loading Large Bitmaps Efficiently

Nitesh
  • 3,868
  • 1
  • 20
  • 26
1

Also try increasing your emulators memory by Editing the emulator. Device Edit

Ashu
  • 639
  • 6
  • 11
  • I'm using a third party emulator called GenyMotion and the program only gives you the option to edit memory/processors and i've tried increasing them by a lot but still no difference. I'll give the android studio AVD a try – Phillip Nov 28 '14 at 23:54
  • I have also tried using GenyMotion but I liked sticking with AVD or mostly actual device with debugging on. – Ashu Nov 29 '14 at 09:12
  • Increasing the heap size worked for the Android Studio AVD I just need to figure out how to do this on the Genymotion emulator now – Phillip Dec 01 '14 at 00:14