1

In my Activity I have the following code:

int picture = Const.a1;
Const.backgroundBitmap = BitmapFactory.decodeResource(getResources(), picture);
Const.backgroundBitmapBonus0 = BitmapFactory.decodeResource(getResources(), Const.bonus_back[0]);
Const.backgroundBitmapBonus1 = BitmapFactory.decodeResource(getResources(), Const.bonus_back[1]);
Const.backgroundBitmapBonus2 = BitmapFactory.decodeResource(getResources(), Const.bonus_back[2]);
Const.backgroundBitmapBonus3 = BitmapFactory.decodeResource(getResources(), Const.bonus_back[3]);

Const.bitmapBackBonus = new Bitmap[4];

Const.bitmapBackBonus[0]= Const.backgroundBitmapBonus0;
Const.bitmapBackBonus[1] = Const.backgroundBitmapBonus1;
Const.bitmapBackBonus[2] = Const.backgroundBitmapBonus2;
Const.bitmapBackBonus[3] = Const.backgroundBitmapBonus3;

And in Const class I have

public static int[] bonus_back = {  R.drawable.back_b0 ,    R.drawable.back_b1 ,    R.drawable.back_b2 ,    R.drawable.back_b3};

When I run the app on my device it crashes, while in AVD it worked fine.

How can I use these pictures in right way?

EDIT:

new code using http://developer.android.com/training/displaying-bitmaps/load-bitmap.html still the same problem

    Const.screenWidth = this.getWindowManager().getDefaultDisplay().getWidth();
    Const.screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();

    int picture = Const.a1; 
    Const.backgroundBitmapBonus0 = decodeSampledBitmapFromResource(getResources(), Const.bonus_back[0], Const.screenWidth, Const.screenHeight);
    Const.backgroundBitmapBonus1 =decodeSampledBitmapFromResource(getResources(), Const.bonus_back[1], Const.screenWidth, Const.screenHeight);
    Const.backgroundBitmapBonus2 = decodeSampledBitmapFromResource(getResources(), Const.bonus_back[2], Const.screenWidth, Const.screenHeight);
    Const.backgroundBitmapBonus3 = decodeSampledBitmapFromResource(getResources(), Const.bonus_back[3], Const.screenWidth, Const.screenHeight);

    Const.backgroundBitmap = decodeSampledBitmapFromResource(getResources(), picture, 100, 100);


    /*Const.backgroundBitmap = BitmapFactory.decodeResource(getResources(), picture);
    Const.backgroundBitmapBonus0 = BitmapFactory.decodeResource(getResources(), Const.bonus_back[0]);
    Const.backgroundBitmapBonus1 = BitmapFactory.decodeResource(getResources(), Const.bonus_back[1]);
    Const.backgroundBitmapBonus2 = BitmapFactory.decodeResource(getResources(), Const.bonus_back[2]);
    Const.backgroundBitmapBonus3 = BitmapFactory.decodeResource(getResources(), Const.bonus_back[3]);
     */
    Const.bitmapBackBonus = new Bitmap[4];

    Const.bitmapBackBonus[0]= Const.backgroundBitmapBonus0;
    Const.bitmapBackBonus[1] = Const.backgroundBitmapBonus1;
    Const.bitmapBackBonus[2] = Const.backgroundBitmapBonus2;
    Const.bitmapBackBonus[3] = Const.backgroundBitmapBonus3;

EDIT 2 : Logcat

11-14 09:45:45.525: D/dalvikvm(5623): Late-enabling CheckJNI
11-14 09:45:45.805: D/skia(5623): ---- fAsset->read(3346) returned 0
11-14 09:45:45.905: D/dalvikvm(5623): GC_FOR_ALLOC freed 99K, 3% free 13923K/14343K, paused 37ms
11-14 09:45:45.905: I/dalvikvm-heap(5623): Grow heap (frag case) to 14.807MB for 992016-byte allocation
11-14 09:45:45.945: D/dalvikvm(5623): GC_CONCURRENT freed 1K, 4% free 14890K/15367K, paused 2ms+2ms
11-14 09:45:46.025: D/dalvikvm(5623): GC_FOR_ALLOC freed 0K, 4% free 14890K/15367K, paused 13ms
11-14 09:45:46.075: I/dalvikvm-heap(5623): Grow heap (frag case) to 29.942MB for 15872016-byte allocation
11-14 09:45:46.115: D/dalvikvm(5623): GC_CONCURRENT freed 0K, 2% free 30390K/30919K, paused 2ms+2ms
11-14 09:45:46.515: D/dalvikvm(5623): GC_FOR_ALLOC freed 969K, 5% free 30391K/31943K, paused 18ms
11-14 09:45:46.565: I/dalvikvm-heap(5623): Grow heap (frag case) to 45.080MB for 15872016-byte allocation
11-14 09:45:46.595: D/dalvikvm(5623): GC_CONCURRENT freed <1K, 4% free 45890K/47495K, paused 2ms+3ms
11-14 09:45:46.905: D/dalvikvm(5623): GC_FOR_ALLOC freed 969K, 4% free 45891K/47495K, paused 15ms
11-14 09:45:46.970: I/dalvikvm-heap(5623): Grow heap (frag case) to 60.216MB for 15872016-byte allocation
11-14 09:45:46.990: D/dalvikvm(5623): GC_FOR_ALLOC freed <1K, 3% free 61391K/63047K, paused 18ms
11-14 09:45:47.050: D/dalvikvm(5623): GC_CONCURRENT freed 0K, 3% free 61391K/63047K, paused 2ms+5ms
11-14 09:45:47.345: D/dalvikvm(5623): GC_FOR_ALLOC freed 969K, 3% free 61391K/63047K, paused 14ms
11-14 09:45:47.345: I/dalvikvm-heap(5623): Forcing collection of SoftReferences for 15872016-byte allocation
11-14 09:45:47.390: D/dalvikvm(5623): GC_BEFORE_OOM freed 9K, 3% free 61382K/63047K, paused 32ms
11-14 09:45:47.390: E/dalvikvm-heap(5623): Out of memory on a 15872016-byte allocation.
11-14 09:45:47.390: I/dalvikvm(5623): "main" prio=5 tid=1 RUNNABLE
11-14 09:45:47.390: I/dalvikvm(5623):   | group="main" sCount=0 dsCount=0 obj=0x40c6d460 self=0x8a9288
11-14 09:45:47.390: I/dalvikvm(5623):   | sysTid=5623 nice=0 sched=0/0 cgrp=default handle=1074558216
11-14 09:45:47.390: I/dalvikvm(5623):   | schedstat=( 1288354466 258529951 3812 ) utm=106 stm=22 core=2
11-14 09:45:47.390: I/dalvikvm(5623):   at android.graphics.Bitmap.nativeCreate(Native Method)
11-14 09:45:47.390: I/dalvikvm(5623):   at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
11-14 09:45:47.390: I/dalvikvm(5623):   at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
11-14 09:45:47.390: I/dalvikvm(5623):   at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
11-14 09:45:47.390: I/dalvikvm(5623):   at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:618)
11-14 09:45:47.390: I/dalvikvm(5623):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:593)
11-14 09:45:47.390: I/dalvikvm(5623):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:445)
11-14 09:45:47.390: I/dalvikvm(5623):   at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:468)
11-14 09:45:47.390: I/dalvikvm(5623):   at com.example.fishe.FullscreenActivity.decodeSampledBitmapFromResource(FullscreenActivity.java:248)
11-14 09:45:47.390: I/dalvikvm(5623):   at com.example.fishe.FullscreenActivity.onCreate(FullscreenActivity.java:142)
11-14 09:45:47.390: I/dalvikvm(5623):   at android.app.Activity.performCreate(Activity.java:4533)
11-14 09:45:47.395: I/dalvikvm(5623):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
11-14 09:45:47.395: I/dalvikvm(5623):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
11-14 09:45:47.395: I/dalvikvm(5623):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
11-14 09:45:47.395: I/dalvikvm(5623):   at android.app.ActivityThread.access$600(ActivityThread.java:128)
11-14 09:45:47.395: I/dalvikvm(5623):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
11-14 09:45:47.395: I/dalvikvm(5623):   at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 09:45:47.395: I/dalvikvm(5623):   at android.os.Looper.loop(Looper.java:137)
11-14 09:45:47.395: I/dalvikvm(5623):   at android.app.ActivityThread.main(ActivityThread.java:4517)
11-14 09:45:47.395: I/dalvikvm(5623):   at java.lang.reflect.Method.invokeNative(Native Method)
11-14 09:45:47.395: I/dalvikvm(5623):   at java.lang.reflect.Method.invoke(Method.java:511)
11-14 09:45:47.395: I/dalvikvm(5623):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
11-14 09:45:47.395: I/dalvikvm(5623):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
11-14 09:45:47.395: I/dalvikvm(5623):   at dalvik.system.NativeStart.main(Native Method)
11-14 09:45:47.395: D/AndroidRuntime(5623): Shutting down VM
11-14 09:45:47.395: W/dalvikvm(5623): threadid=1: thread exiting with uncaught exception (group=0x40c6c1f8)
11-14 09:45:47.395: E/AndroidRuntime(5623): FATAL EXCEPTION: main
11-14 09:45:47.395: E/AndroidRuntime(5623): java.lang.OutOfMemoryError
11-14 09:45:47.395: E/AndroidRuntime(5623):     at android.graphics.Bitmap.nativeCreate(Native Method)
11-14 09:45:47.395: E/AndroidRuntime(5623):     at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
11-14 09:45:47.395: E/AndroidRuntime(5623):     at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
11-14 09:45:47.395: E/AndroidRuntime(5623):     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
11-14 09:45:47.395: E/AndroidRuntime(5623):     at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:618)
11-14 09:45:47.395: E/AndroidRuntime(5623):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:593)
11-14 09:45:47.395: E/AndroidRuntime(5623):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:445)
11-14 09:45:47.395: E/AndroidRuntime(5623):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:468)
11-14 09:45:47.395: E/AndroidRuntime(5623):     at com.example.fishe.FullscreenActivity.decodeSampledBitmapFromResource(FullscreenActivity.java:248)
11-14 09:45:47.395: E/AndroidRuntime(5623):     at com.example.fishe.FullscreenActivity.onCreate(FullscreenActivity.java:142)
11-14 09:45:47.395: E/AndroidRuntime(5623):     at android.app.Activity.performCreate(Activity.java:4533)
11-14 09:45:47.395: E/AndroidRuntime(5623):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
11-14 09:45:47.395: E/AndroidRuntime(5623):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
11-14 09:45:47.395: E/AndroidRuntime(5623):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
11-14 09:45:47.395: E/AndroidRuntime(5623):     at android.app.ActivityThread.access$600(ActivityThread.java:128)
11-14 09:45:47.395: E/AndroidRuntime(5623):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
11-14 09:45:47.395: E/AndroidRuntime(5623):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 09:45:47.395: E/AndroidRuntime(5623):     at android.os.Looper.loop(Looper.java:137)
11-14 09:45:47.395: E/AndroidRuntime(5623):     at android.app.ActivityThread.main(ActivityThread.java:4517)
11-14 09:45:47.395: E/AndroidRuntime(5623):     at java.lang.reflect.Method.invokeNative(Native Method)
11-14 09:45:47.395: E/AndroidRuntime(5623):     at java.lang.reflect.Method.invoke(Method.java:511)
11-14 09:45:47.395: E/AndroidRuntime(5623):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
11-14 09:45:47.395: E/AndroidRuntime(5623):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
11-14 09:45:47.395: E/AndroidRuntime(5623):     at dalvik.system.NativeStart.main(Native Method)
11-14 09:45:56.490: I/Process(5623): Sending signal. PID: 5623 SIG: 9
GO VEGAN
  • 1,113
  • 3
  • 15
  • 44
  • there must be some logcat. If you post those then it would be easy to track down the cause. – surhidamatya Nov 13 '13 at 10:37
  • In the avd it work but when i play it in my device it doesn't and I don't succed to run it in debug state so I firstly coping it to my device and then run it – GO VEGAN Nov 13 '13 at 10:43
  • why you can't run it on your device? – mihail Nov 13 '13 at 10:44
  • it's doesn't appear in the options (I enabled debug in the device , and connect the device to computer using usb) – GO VEGAN Nov 13 '13 at 11:00
  • you have not installed your USB drivers - have a look here http://stackoverflow.com/questions/7510471/problem-when-connecting-my-device-to-pc . – mihail Nov 13 '13 at 11:47

1 Answers1

0

If the image is too big you're probably running out of memory.

Check http://developer.android.com/training/displaying-bitmaps/load-bitmap.html to learn how to load a scaled-down version of the image.

M Rajoy
  • 4,028
  • 14
  • 54
  • 111
  • I chnged now the code like the exmaple above above and still the same probelm. I will edit my question with the new code – GO VEGAN Nov 13 '13 at 11:21
  • Then you will need to post your LogCat to see what exaclty is causing the error. In Eclipse activate LogCat view goind to Window->Show View->LogCat. – M Rajoy Nov 13 '13 at 11:23
  • If I understand your code correctly, you are loading 4 bitmaps with a width and height equal to your screen. You probably are not showing them at the same time so you should manage their loading as you need them. There's only so much memory on your device so you're going out of memory by loading so many big bitmaps. – M Rajoy Nov 14 '13 at 08:00
  • In fact, you can see how you run out of memory when loading the bitmaps. Check the subsequent 15872016-byte allocations in your logcat before the exception and you'll see how the memory increases up until the device runs out of it. – M Rajoy Nov 14 '13 at 08:53
  • I want to make the background to change every second with 4 frames. So what is the best way to it? – GO VEGAN Nov 14 '13 at 14:16
  • on each cycle unload the previous Bitmap and load the new one. – M Rajoy Nov 14 '13 at 14:19
  • Is bitmap = null for unload is enough? – GO VEGAN Nov 14 '13 at 15:44
  • should be but try using the same object and calling bitmap.recycle() before the new assignment. – M Rajoy Nov 14 '13 at 16:28