1

可能重复:
Android:将图像加载到位图对象时出现奇怪的内存不足问题

我正在创建一个游戏。它工作得很好,但有时它会抛出这样的消息:Out of memory on a 1024016-byte allocation!我不明白为什么android确实不能分配1MB的内存?我正在使用BitmapFactory.decodeResource(res, id)功能。如果位图变得无用,我会立即回收它!我什至减小了位图的大小!如果有人知道如何解决此问题,我将不胜感激!LogCat 下面:

10-11 09:56:28.312 I/dalvikvm(13613): "main" prio=5 tid=1 RUNNABLE
10-11 09:56:28.312 I/dalvikvm(13613):   | group="main" sCount=0 dsCount=0 obj=0x40a32460 self=0x18e5828
10-11 09:56:28.312 I/dalvikvm(13613):   | sysTid=13613 nice=0 sched=0/0 cgrp=default handle=1075090664
10-11 09:56:28.312 I/dalvikvm(13613):   | schedstat=( 0 0 0 ) utm=788 stm=85 core=0
10-11 09:56:28.320 I/dalvikvm(13613):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
10-11 09:56:28.320 I/dalvikvm(13613):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483)
10-11 09:56:28.320 I/dalvikvm(13613):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
10-11 09:56:28.320 I/dalvikvm(13613):   at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:374)
10-11 09:56:28.320 I/dalvikvm(13613):   at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:404)
10-11 09:56:28.320 I/dalvikvm(13613):   at mygames.airdefense.extensions.FunctionalHelper.getBmpFromRes(FunctionalHelper.java:30)
10-11 09:56:28.320 I/dalvikvm(13613):   at mygames.airdefense.weathereffect.Wind.<init>(Wind.java:37)
10-11 09:56:28.320 I/dalvikvm(13613):   at mygames.airdefense.game.GameEngineMultiPlayer.InitObjects(GameEngineMultiPlayer.java:268)
10-11 09:56:28.320 I/dalvikvm(13613):   at mygames.airdefense.game.GameEngineMultiPlayer.<init>(GameEngineMultiPlayer.java:170)
10-11 09:56:28.320 I/dalvikvm(13613):   at mygames.airdefense.game.GameActivity.onCreate(GameActivity.java:189)
10-11 09:56:28.320 I/dalvikvm(13613):   at android.app.Activity.performCreate(Activity.java:4465)
10-11 09:56:28.320 I/dalvikvm(13613):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-11 09:56:28.320 I/dalvikvm(13613):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
10-11 09:56:28.320 I/dalvikvm(13613):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-11 09:56:28.320 I/dalvikvm(13613):   at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3351)
10-11 09:56:28.320 I/dalvikvm(13613):   at android.app.ActivityThread.access$700(ActivityThread.java:123)
10-11 09:56:28.320 I/dalvikvm(13613):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
10-11 09:56:28.320 I/dalvikvm(13613):   at android.os.Handler.dispatchMessage(Handler.java:99)
10-11 09:56:28.320 I/dalvikvm(13613):   at android.os.Looper.loop(Looper.java:137)
10-11 09:56:28.320 I/dalvikvm(13613):   at android.app.ActivityThread.main(ActivityThread.java:4424)
10-11 09:56:28.320 I/dalvikvm(13613):   at java.lang.reflect.Method.invokeNative(Native Method)
10-11 09:56:28.320 I/dalvikvm(13613):   at java.lang.reflect.Method.invoke(Method.java:511)
10-11 09:56:28.320 I/dalvikvm(13613):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-11 09:56:28.320 I/dalvikvm(13613):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-11 09:56:28.320 I/dalvikvm(13613):   at dalvik.system.NativeStart.main(Native Method)
10-11 09:56:28.320 I/dalvikvm(13613): 
10-11 09:56:28.328 D/skia    (13613): --- decoder->decode returned false
10-11 09:56:28.335 D/AndroidRuntime(13613): Shutting down VM
10-11 09:56:28.335 W/dalvikvm(13613): threadid=1: thread exiting with uncaught exception (group=0x40a311f8)
10-11 09:56:28.351 E/AndroidRuntime(13613): FATAL EXCEPTION: main
10-11 09:56:28.351 E/AndroidRuntime(13613): java.lang.OutOfMemoryError
10-11 09:56:28.351 E/AndroidRuntime(13613):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:374)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:404)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at mygames.airdefense.extensions.FunctionalHelper.getBmpFromRes(FunctionalHelper.java:30)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at mygames.airdefense.weathereffect.Wind.<init>(Wind.java:37)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at mygames.airdefense.game.GameEngineMultiPlayer.InitObjects(GameEngineMultiPlayer.java:268)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at mygames.airdefense.game.GameEngineMultiPlayer.<init>(GameEngineMultiPlayer.java:170)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at mygames.airdefense.game.GameActivity.onCreate(GameActivity.java:189)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at android.app.Activity.performCreate(Activity.java:4465)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3351)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at android.app.ActivityThread.access$700(ActivityThread.java:123)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at android.os.Looper.loop(Looper.java:137)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at android.app.ActivityThread.main(ActivityThread.java:4424)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at java.lang.reflect.Method.invokeNative(Native Method)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at java.lang.reflect.Method.invoke(Method.java:511)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-11 09:56:28.351 E/AndroidRuntime(13613):     at dalvik.system.NativeStart.main(Native Method)
10-11 09:56:28.367 W/ActivityManager(  185):   Force finishing activity mygames.airdefense/.game.GameActivity
4

1 回答 1

0
public static Bitmap getBmpFromRes(Resources res, int id){  
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 1; // sample size value depends upon your image  so you have to              calculate it on run time.
    options.inPurgeable = true; //IMP
   return  BitmapFactory.decodeResource(res, id,options); }
于 2012-10-16T11:49:26.153 回答