Android在加载小图像Drawable时给出内存不足

时间:2012-06-28 20:17:21

标签: android bitmap drawable out-of-memory

我想知道在视图中使用图像绘制文件时如何将它们加载到内存中。

我的问题是我有一个大尺寸的图像(约600x700),仅重4kb。我认为没有必要创建一个9补丁图像,因为图像足够小。但是,当我尝试将此图像用作背景时,Android会给我内存错误。

06-28 14:54:09.359: E/dalvikvm-heap(2272): Out of memory on a 6816336-byte allocation.
06-28 14:54:09.359: I/dalvikvm(2272): "main" prio=5 tid=1 RUNNABLE
06-28 14:54:09.359: I/dalvikvm(2272):   | group="main" sCount=0 dsCount=0 obj=0x40a5d460 self=0x12828
06-28 14:54:09.359: I/dalvikvm(2272):   | sysTid=2272 nice=0 sched=0/0 cgrp=default handle=1074603144
06-28 14:54:09.359: I/dalvikvm(2272):   | schedstat=( 0 0 0 ) utm=162 stm=18 core=0
06-28 14:54:09.359: I/dalvikvm(2272):   at android.graphics.Bitmap.nativeCreate(Native Method)
06-28 14:54:09.359: I/dalvikvm(2272):   at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
06-28 14:54:09.359: I/dalvikvm(2272):   at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
06-28 14:54:09.359: I/dalvikvm(2272):   at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
06-28 14:54:09.359: I/dalvikvm(2272):   at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:524)
06-28 14:54:09.359: I/dalvikvm(2272):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
06-28 14:54:09.359: I/dalvikvm(2272):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
06-28 14:54:09.359: I/dalvikvm(2272):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
06-28 14:54:09.359: I/dalvikvm(2272):   at android.content.res.Resources.loadDrawable(Resources.java:1937)
06-28 14:54:09.359: I/dalvikvm(2272):   at android.content.res.Resources.getDrawable(Resources.java:664)
06-28 14:54:09.359: I/dalvikvm(2272):   at android.view.View.setBackgroundResource(View.java:11588)
06-28 14:54:09.359: I/dalvikvm(2272):   at com.telcel.olmpiadas2012.gui.viewcontrollers.medals.MedalsDetail.<init>(MedalsDetail.java:14)
06-28 14:54:09.359: I/dalvikvm(2272):   at com.telcel.olmpiadas2012.gui.viewcontrollers.medals.MedalsHomeVC.onCreate(MedalsHomeVC.java:53)
06-28 14:54:09.359: I/dalvikvm(2272):   at android.app.Activity.performCreate(Activity.java:4465)
06-28 14:54:09.359: I/dalvikvm(2272):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-28 14:54:09.367: I/dalvikvm(2272):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
06-28 14:54:09.367: I/dalvikvm(2272):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
06-28 14:54:09.367: I/dalvikvm(2272):   at android.app.ActivityThread.access$600(ActivityThread.java:122)
06-28 14:54:09.367: I/dalvikvm(2272):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
06-28 14:54:09.367: I/dalvikvm(2272):   at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 14:54:09.367: I/dalvikvm(2272):   at android.os.Looper.loop(Looper.java:137)
06-28 14:54:09.367: I/dalvikvm(2272):   at android.app.ActivityThread.main(ActivityThread.java:4340)
06-28 14:54:09.367: I/dalvikvm(2272):   at java.lang.reflect.Method.invokeNative(Native Method)
06-28 14:54:09.367: I/dalvikvm(2272):   at java.lang.reflect.Method.invoke(Method.java:511)
06-28 14:54:09.367: I/dalvikvm(2272):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-28 14:54:09.367: I/dalvikvm(2272):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-28 14:54:09.367: I/dalvikvm(2272):   at dalvik.system.NativeStart.main(Native Method)

我添加了其他大图像作为背景没有问题,我怀疑我的应用程序占用了除4kb以外的所有内存空间,因为其他活动加载了比这个更多的资源,我可以轻松访问它们。

我应该考虑其他方面而不是最终图像字节大小吗?

1 个答案:

答案 0 :(得分:0)

Bitmap未压缩,600x700尺寸使内存非常大,如果质量不是问题,可以尝试将其缩小。

编辑:

600 * 700 * 32/8 = 1680000