Android outofmemory 错误位图大小超过 2.3.3 中的 vm 预算

标签 android android-emulator android-layout

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

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

注释。

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

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

[更新] - fragment

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: ERROR/dalvikvm-heap(5883): 4320000-byte external allocation too large for this process. 03-21 08:59:17.412: ERROR/GraphicsJNI(5883): VM won't let us allocate 4320000 bytes 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): FATAL EXCEPTION: main 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): java.lang.OutOfMemoryError: bitmap size exceeds VM budget 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.graphics.Bitmap.nativeCreate(Native Method) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.graphics.Bitmap.createBitmap(Bitmap.java:477) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.graphics.Bitmap.createBitmap(Bitmap.java:444) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.content.res.Resources.loadDrawable(Resources.java:1709) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.content.res.Resources.getDrawable(Resources.java:581) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.widget.ImageView.resolveUri(ImageView.java:501) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.widget.ImageView.setImageResource(ImageView.java:280) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at Quiz.java:124) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): Quiz.onResume(Quiz.java:92) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.app.Activity.performResume(Activity.java:3832) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2110) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.os.Handler.dispatchMessage(Handler.java:99) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.os.Looper.loop(Looper.java:123) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at android.app.ActivityThread.main(ActivityThread.java:3683) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at java.lang.reflect.Method.invokeNative(Native Method) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at java.lang.reflect.Method.invoke(Method.java:507) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 03-21 08:59:17.432: ERROR/AndroidRuntime(5883): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 03-21 08:59:17.432: ERROR/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));

最佳答案

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

你可以使用这个 fragment 来调整位图的大小

http://pastebin.com/D8vbQd2u

关于Android outofmemory 错误位图大小超过 2.3.3 中的 vm 预算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5375141/

相关文章:

android - 查看 Xml 的图形布局时 Eclipse 中的 XML 错误

java - 获取 xml 布局中项目的 ID

android - 具有多个项目选取框的回收站 View

android - 如何在 Android 中创建一个对所有应用程序具有读写权限的共享目录?

android - Radio Group - 获取选定的 ID

java - Android - 根据音量计算分贝

android - 获取 TabLayout 选项卡的 TextView

android - 将系统应用程序替换为您自己在android上修改的应用程序

android - 在 Android EditText 中,如何从软键盘中删除建议栏?

java - 如何在 ListView 中使用新的自定义 View ?