OutOfMemoryError,我不知道何时或为何发生

时间:2016-07-09 20:08:37

标签: android android-bitmap android-memory

所以我刚刚注意到Crashlytics的这次崩溃。显然它发生了4次到3个不同的用户。其中一个有一个相当不错的设备(Nexus 5)。

堆栈跟踪:

Fatal Exception: java.lang.OutOfMemoryError: Failed to allocate a 4194316 byte allocation with 3271760 free bytes and 3MB until OOM
   at dalvik.system.VMRuntime.newNonMovableArray(VMRuntime.java)
   at android.graphics.Bitmap.nativeCreate(Bitmap.java)
   at android.graphics.Bitmap.createBitmap(Bitmap.java:831)
   at android.graphics.Bitmap.createBitmap(Bitmap.java:808)
   at android.graphics.Bitmap.createBitmap(Bitmap.java:775)
   at maps.aa.g.a(Unknown Source)
   at maps.aa.i.a(Unknown Source)
   at maps.aa.i.a(Unknown Source)
   at maps.aa.i.b(Unknown Source)
   at maps.ac.o.a(Unknown Source)
   at maps.ac.t.a(Unknown Source)
   at maps.X.K.a(Unknown Source)
   at maps.X.H.a(Unknown Source)
   at maps.X.H.b(Unknown Source)
   at maps.X.y$f.f(Unknown Source)
   at maps.X.y$f.run(Unknown Source)

从这个堆栈跟踪中,我无法弄清楚此次崩溃发生的时间或原因。

我在我的应用中的两个不同位置使用Bitmaps。这些是我的方法。

加载一个drawable,然后在其上绘制一些文本:

public Bitmap getMarkerBitmap(Context context) {
    Resources resources = context.getResources();
    float scale = resources.getDisplayMetrics().density;
    Bitmap bitmap = null;
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    String text = Integer.toString(reservableCars);
    paint.setColor(Color.WHITE);

    if (reservableCars == 0) {
        bitmap = BitmapFactory.decodeResource(resources, R.drawable.marker_nocars);
    } else if (isPublic()) {
        bitmap = BitmapFactory.decodeResource(resources, R.drawable.marker_public_location_cars);
    } else {
        bitmap = BitmapFactory.decodeResource(resources, R.drawable.marker_corpo_location_cars);
    }

    android.graphics.Bitmap.Config bitmapConfig =
            bitmap.getConfig();
    // set default bitmap config if none
    if(bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }
    // resource bitmaps are imutable,
    // so we need to convert it to mutable one
    bitmap = bitmap.copy(bitmapConfig, true);

    Canvas canvas = new Canvas(bitmap);
    // text size in pixels
    paint.setTextSize((int) (10 * scale));
    // text shadow
    paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

    // draw text to the Canvas center
    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    int x = (bitmap.getWidth() - bounds.width())/2;
    int y = (bitmap.getHeight() + bounds.height())/2;

    canvas.drawText(text, x, y, paint);

    return bitmap;
}

画一个圆圈和两个字母。

public Bitmap drawProfileBitmap(Context context) {
    Paint paint = new Paint();
    Paint circlePaint = new Paint();
    Rect bounds = new Rect();
    String text = Character.toString(firstName.charAt(0)) + Character.toString(lastName.charAt(0));
    int pixelvalue = (int) (70 * context.getResources().getDisplayMetrics().density * 0.5f);
    Bitmap bitmap = Bitmap.createBitmap(pixelvalue, pixelvalue, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);

    paint.setColor(Color.WHITE);
    paint.setTextSize(40f);
    paint.setAntiAlias(true);
    paint.setTextAlign(Paint.Align.CENTER);
    paint.getTextBounds(text, 0, text.length(), bounds);

    circlePaint.setColor(context.getResources().getColor(R.color.primary));
    circlePaint.setAntiAlias(true);

    canvas.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2, bitmap.getHeight(), circlePaint);
    canvas.drawText(text, bitmap.getWidth()/2, (bitmap.getHeight() + bounds.height())/2, paint);
    return bitmap;
}

由于上述方法之一,这次崩溃会发生吗?第一个叫多次。对于在Google地图上绘制的每个标记 - 但地图上的标记从不超过15-20个。

每次应用程序启动时,第二个只调用一次。

1 个答案:

答案 0 :(得分:0)

尝试在未使用

时解压缩位图
CoGroupFunction

也(如果可能),您需要删除指向您位图的所有链接。 for ex在使用Image视图的情况下:mImageView.setImageBitmap(null);等等。