在我的活动中,我有以下代码:
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;
在Const
课堂上我有
public static int[] bonus_back = { R.drawable.back_b0 , R.drawable.back_b1 , R.drawable.back_b2 , R.drawable.back_b3};
当我在我的设备上运行该应用程序时它崩溃了,而在 AVD 中它运行良好。
我怎样才能以正确的方式使用这些图片?
编辑:
使用http://developer.android.com/training/displaying-bitmaps/load-bitmap.html的新代码 仍然是同样的问题
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;
编辑 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