1

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;
}
Nguyen Minh Binh
  • 23,891
  • 30
  • 115
  • 165
  • 1
    Sometimes, the errormessage 'out of memory' appears, when the lack of memory is not really the cause of the problem but something else. Can you specify the stack trace and/or errormessage? – real_yggdrasil Feb 27 '12 at 07:44
  • 1
    My answer posted in another similar thread might help you: http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object/18544069#18544069 – Bruce Sep 16 '13 at 01:58

2 Answers2

1

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

melle
  • 186
  • 2
  • 6
-4
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
    bitmap.getHeight(), new BitmapFactory.Options().inSampleSize=4);

Try above code to create bitmap.

Vikash Kumar
  • 621
  • 2
  • 6
  • 21