位图加载outofmemory android

时间:2013-11-20 14:08:48

标签: android bitmap

我正在制作一个具有不同位图的壁纸,这些位图使用矩阵移动和旋转。现在,我有8个位图,大小约为300 X 300,作为pngs。我需要他们一直在屏幕上。我在onCreate中使用它:

sun = BitmapFactory.decodeResource(getResources(), R.drawable.sun);
            planet_1 = BitmapFactory.decodeResource(getResources(),
                    R.drawable.planet_1);
            scene_1 = BitmapFactory.decodeResource(getResources(),
                    R.drawable.full_scaled);
            planet_2 = BitmapFactory.decodeResource(getResources(),
                    R.drawable.planet_2);
            scene_2 = BitmapFactory.decodeResource(getResources(),
                    R.drawable.scene_2);
            planet_3 = BitmapFactory.decodeResource(getResources(),
                    R.drawable.planet_3);
            scene_3 = BitmapFactory.decodeResource(getResources(),
                    R.drawable.scene_3);
            bg = BitmapFactory.decodeResource(getResources(), R.drawable.bg);

它们会在预览中加载,但是当我尝试设置壁纸时,它会崩溃:

11-20 09:06:55.358: E/dalvikvm(6924): adjustAdaptiveCoef max=6291456, min=1572864, ut=568
11-20 09:06:55.389: E/dalvikvm(6924): adjustAdaptiveCoef max=8388608, min=2097152, ut=368
11-20 09:06:55.429: E/dalvikvm(6924): adjustAdaptiveCoef max=8388608, min=2097152, ut=256
11-20 09:06:55.899: E/dalvikvm(6924): adjustAdaptiveCoef max=4194304, min=1048576, ut=568
11-20 09:06:55.919: E/dalvikvm(6924): adjustAdaptiveCoef max=6291456, min=1572864, ut=368
11-20 09:06:56.009: E/dalvikvm(6924): adjustAdaptiveCoef max=8388608, min=2097152, ut=256
11-20 09:06:59.052: E/dalvikvm(6924): adjustAdaptiveCoef max=4194304, min=1048576, ut=568
11-20 09:06:59.122: E/dalvikvm(6924): adjustAdaptiveCoef max=6291456, min=1572864, ut=368
11-20 09:06:59.192: E/dalvikvm(6924): adjustAdaptiveCoef max=8388608, min=2097152, ut=256
11-20 09:06:59.452: E/dalvikvm(6924): adjustAdaptiveCoef max=4194304, min=1048576, ut=568
11-20 09:06:59.483: E/dalvikvm(6924): adjustAdaptiveCoef max=6291456, min=1572864, ut=368
11-20 09:06:59.483: E/dalvikvm-heap(6924): Out of memory on a 4088500-byte allocation.
11-20 09:06:59.503: E/AndroidRuntime(6924): FATAL EXCEPTION: main
11-20 09:06:59.503: E/AndroidRuntime(6924): java.lang.OutOfMemoryError
11-20 09:06:59.503: E/AndroidRuntime(6924):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
11-20 09:06:59.503: E/AndroidRuntime(6924):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
11-20 09:06:59.503: E/AndroidRuntime(6924):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
11-20 09:06:59.503: E/AndroidRuntime(6924):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:472)
11-20 09:06:59.503: E/AndroidRuntime(6924):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:502)
11-20 09:06:59.503: E/AndroidRuntime(6924):     at com.bdcorps.PlanetMe.PlanetMain$StripedEngine.onCreate(PlanetMain.java:169)
11-20 09:06:59.503: E/AndroidRuntime(6924):     at android.service.wallpaper.WallpaperService$Engine.attach(WallpaperService.java:778)
11-20 09:06:59.503: E/AndroidRuntime(6924):     at android.service.wallpaper.WallpaperService$IWallpaperEngineWrapper.executeMessage(WallpaperService.java:1038)
11-20 09:06:59.503: E/AndroidRuntime(6924):     at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:40)
11-20 09:06:59.503: E/AndroidRuntime(6924):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 09:06:59.503: E/AndroidRuntime(6924):     at android.os.Looper.loop(Looper.java:137)
11-20 09:06:59.503: E/AndroidRuntime(6924):     at android.app.ActivityThread.main(ActivityThread.java:5283)
11-20 09:06:59.503: E/AndroidRuntime(6924):     at java.lang.reflect.Method.invokeNative(Native Method)
11-20 09:06:59.503: E/AndroidRuntime(6924):     at java.lang.reflect.Method.invoke(Method.java:511)
11-20 09:06:59.503: E/AndroidRuntime(6924):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
11-20 09:06:59.503: E/AndroidRuntime(6924):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
11-20 09:06:59.503: E/AndroidRuntime(6924):     at dalvik.system.NativeStart.main(Native Method)

ANy帮助?

2 个答案:

答案 0 :(得分:0)

参考this Documention,您将清楚了解有效显示位图

答案 1 :(得分:0)

你可以试试这样的东西

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferredConfig = Bitmap.Config.RGB_565; // or Bitmap.Config.ARGB_4444
sun = BitmapFactory.decodeResource(getResources(), R.drawable.sun, opts);

这是最简单的方法,值得一试。当然,这将使你的图形质量更差,但取决于它们的样子你甚至可能看不出差异。