【问题标题】:Android outofmemory error bitmap size exceeds vm budget in 2.3.3Android outofmemory 错误位图大小超出 2.3.3 中的 vm 预算
【发布时间】:2011-07-19 12:13:47
【问题描述】:

我知道这个问题被问过几次。他们都不清楚解决方案。让我解释一下这个问题。

  1. 我有一个一次加载 4 张图像的 Activity。
  2. 我在 onResume() 方法中加载图像。
  3. Activity 在加载时抛出位图错误。

注释。

  1. 我正在使用 setImageResource(R.drawable.xxxx) 方法调用设置图像,而不是直接使用位图/drawables。
  2. 图像已正确缩放。
  3. 该活动在 2.3 之前的所有模拟器中运行良好,并且在实际设备中运行良好 (Samsung Galaxy 5)
  4. 第一次初始化时出现错误,并且没有触发方向更改事件。
  5. 图片大小为 800 x 600,平均大小为 15kb(每张)。

让我知道任何解决方案。如果您对 Android 2.3.3 模拟器有类似问题,请告诉我。

[更新]-sn-ps

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
            ...
    img_topLeft = (ImageView) findViewById(R.id.Img_Alph_Q_TopLeft);
    img_topRight = (ImageView) findViewById(R.id.Img_Alph_Q_TopRight);
    img_bottomLeft = (ImageView) findViewById(R.id.Img_Alph_Q_BottomLeft);
    img_bottomRight = (ImageView) findViewById(R.id.Img_Alph_Q_BottomRight);
   ...
   }
protected void onResume() {
    super.onResume();
            img_topLeft.setImageResource(R.drawable.xxx);
            img_topRight.setImageResource(R.drawable.xxx);
            img_bottomLeft.setImageResource(R.drawable.xxx);
            img_bottomRight.setImageResource(R.drawable.xxx);
   ...
   }

03-21 08:59:17.362: 错误/dalvikvm-heap(5883): 4320000 字节的外部分配对于这个进程来说太大了。 03-21 08:59:17.412: 错误/GraphicsJNI(5883): VM 不允许我们分配 4320000 字节 03-21 08:59:17.432:错误/AndroidRuntime(5883):致命异常:主要 03-21 08:59:17.432: 错误/AndroidRuntime(5883): java.lang.OutOfMemoryError: 位图大小超过 VM 预算 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 android.graphics.Bitmap.nativeCreate(Native Method) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 android.graphics.Bitmap.createBitmap(Bitmap.java:477) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 android.graphics.Bitmap.createBitmap(Bitmap.java:444) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 android.content.res.Resources.loadDrawable(Resources.java:1709) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 android.content.res.Resources.getDrawable(Resources.java:581) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 android.widget.ImageView.resolveUri(ImageView.java:501) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 android.widget.ImageView.setImageResource(ImageView.java:280) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 Quiz.java:124) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): Quiz.onResume(Quiz.java:92) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 android.app.Activity.performResume(Activity.java:3832) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 android.app.ActivityThread.performResumeActivity(ActivityThread.java:2110) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 android.app.ActivityThread.access$1500(ActivityThread.java:117) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 android.os.Handler.dispatchMessage(Handler.java:99) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 android.os.Looper.loop(Looper.java:123) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 android.app.ActivityThread.main(ActivityThread.java:3683) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 java.lang.reflect.Method.invokeNative(Native Method) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 java.lang.reflect.Method.invoke(Method.java:507) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 03-21 08:59:17.432: 错误/AndroidRuntime(5883): at dalvik.system.NativeStart.main(Native Method)

谢谢。设法解决它。共享代码以造福他人 解决此问题的自定义类。基于@Janardhanan.S 链接。

public class BitmapResizer {

public static Bitmap decodeImage(Resources res, int id ,int requiredSize){
    try {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, id, o);

        //Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE=requiredSize;
        int width_tmp=o.outWidth, height_tmp=o.outHeight;
        int scale=1;
        while(true){
            if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                break;
            width_tmp/=2;
            height_tmp/=2;
            scale*=2;
        }

        //decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeResource(res, id, o2);
    } catch (Exception e) {

    }
    return null;
}

}

//Class call
int requiredsize = 100; // Still playing around with this number to find the optimum value
img_topLeft.setImageBitmap(BitmapResizer.decodeImage(getResources(),
        AlphResourceSet.R.drawable.xxx, requiredsize));

【问题讨论】:

  • 发布您的 onResume 代码,它可能更容易为您提供帮助。

标签: android android-emulator android-layout


【解决方案1】:

你能贴出Activity + stacktrace的代码sn-p吗?

您是否已经检查了Avoiding memory leaks article

尤其是以下部分:

当一个 Drawable 被附加到一个视图时,该视图被设置为一个可绘制对象的回调。在上面的代码 sn-p 中,这意味着可绘制对象具有对 TextView 的引用,该 TextView 本身具有对活动(上下文)的引用,而活动(上下文)又引用了几乎所有内容(取决于您的代码)。

【讨论】:

  • 我检查了链接。不确定我的代码是否可以做很多事情。将代码 sn-p 更新为问题。请检查并让我知道我是否需要在此基础上进行一些修改。
【解决方案2】:

这些图像看起来很重。我建议启动实际返回位图实例的 AsyncTimer,并在 AsyncTask 的 doBackground() 中传递图像 id 并使用 Bitmap 类的createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter) 方法将其重新缩放为所需的大小,例如 32 x 32。

【讨论】:

  • 图片大小为 800 x 600,平均大小为 15kb(每个)。你能发布一些关于如何做 AsyncTimer 的 sn-p 吗?
【解决方案3】:

位图占用大量内存空间。 不要为您在活动中加载的所有图像创建新的位图变量, 相反,您可以创建一个位图变量并尽可能多地重复使用它们。

你可以使用这个 sn-p 来调整位图的大小

http://pastebin.com/D8vbQd2u

【讨论】:

  • 考虑到我没有使用位图变量,我是否需要处理这个问题?我直接使用可绘制的参考表单。附上代码。
  • 你不需要考虑为小图像调整大小,但一个 Activity 的 64KB 将给 UI 带来压力并消耗内存。将它们转换为位图并将它们提供给 imageview
  • rdhanan.S 什么是 64kb?每个活动是否有图像大小。我的图片大小约为 15kb,采用 png 格式。一项活动将有 4 张图像。所以 ~15 x 4 = ~60Kb。
  • 对不起,我认为图片大小为 16KB.. 我无法编辑上述评论。
  • rdhanan.S 感谢您的链接。我设法编写了一个类似的方法(而不是使用文件,我使用了图像资源)并且运行良好。最后一个问题。您知道“requiredsize”参数的最佳值是多少(基于您提供的链接)。我用了 100。似乎还可以
猜你喜欢
  • 2012-08-09
  • 2012-05-31
  • 1970-01-01
  • 1970-01-01
  • 2012-08-19
  • 1970-01-01
  • 2010-12-07
  • 2010-12-29
相关资源
最近更新 更多