Out of memory during working with Bitmap in android

醉酒当歌 提交于 2020-01-23 21:36:26

问题


I use the method quoted below to round the corners of any bitmap. Some time it works perfectly, some time not because of OutOfMemory exception. The exception often occur at the line

Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
        bitmap.getHeight(), Config.ARGB_8888);

Do I miss any special codes here? Please help me.

 public static Bitmap roundBitmap(Bitmap bitmap, int roundedRadius) {
    if (bitmap == null)
        return null;
    if (roundedRadius == 0) {
        return bitmap;
    }
    // Bitmap output = bitmap.copy(Config.ARGB_8888, true);
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final int color = 0xffffff00;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = roundedRadius;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}

回答1:


If you don't need the original bitmap after calling roundBitmap() you should call bitmap.recycle() to free the memory used by bitmap.




回答2:


Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
    bitmap.getHeight(), new BitmapFactory.Options().inSampleSize=4);

Try above code to create bitmap.



来源:https://stackoverflow.com/questions/9461512/out-of-memory-during-working-with-bitmap-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!