2016-07-09 174 views
1

所以我只注意到Crashlytics的這個崩潰。顯然它發生了4次,3個不同的用戶。其中之一有一個相當不錯的設備(Nexus 5)。OutOfMemoryError,我不知道什麼時候或爲什麼發生

堆棧跟蹤:

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) 

從這個堆棧跟蹤,我只是想不通的時候或者爲什麼這種崩潰發生。

我在我的應用程序的兩個不同位置使用位圖。這些是我的方法。

負載被拉伸,然後借鑑它的一些文字:

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個標記。

第二個應用程序每次啓動時只調用一次。

回答

0

嘗試recucle位圖時,它不使用

if (mBitmap != null) mBitmap.recycle(); 
    mBitmap = null; 

也(如果可能),你需要刪除所有鏈接到你的位圖。在使用Image view的情況下爲ex ex: :mImageView.setImageBitmap(null);等等。

相關問題