1

OOM Exception This is code iam useing

@Override
protected void onDraw(Canvas canvas) {

    Drawable drawable = getDrawable();

    if (drawable == null) {
        return;
    }

    if (getWidth() == 0 || getHeight() == 0) {
        return;
    }
    Bitmap b = ((BitmapDrawable) drawable).getBitmap();
    if (b == null) {
        Log.d("null", "yes");
    } else {
        Log.d("not null", "yes");
    }
    Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);

    int w = getWidth(), h = getHeight();
    Bitmap roundBitmap = getCroppedBitmap(bitmap, w);

    canvas.drawBitmap(roundBitmap, 0, 0, null);

}

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
    Bitmap sbmp;
    if (bmp.getWidth() != radius || bmp.getHeight() != radius)
        sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
    else
        sbmp = bmp;
    Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(),
            Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xffa19774;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());

    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#BAB399"));
    canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f,
            sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(sbmp, rect, rect, paint);

    return output;
}

this is track trace

 E/AndroidRuntime(10555): FATAL EXCEPTION: main
 E/AndroidRuntime(10555): java.lang.OutOfMemoryError
 E/AndroidRuntime(10555):   at android.graphics.Bitmap.nativeCreate(Native Method)
 E/AndroidRuntime(10555):   at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
 E/AndroidRuntime(10555):   at android.graphics.Bitmap.createBitmap(Bitmap.java:620)
 E/AndroidRuntime(10555):   at com.blsk.bigtoss.RoundedImageView.getCroppedBitmap(RoundedImageView.java:68)
 E/AndroidRuntime(10555):   at com.blsk.bigtoss.RoundedImageView.onDraw(RoundedImageView.java:55)
 E/AndroidRuntime(10555):   at android.view.View.draw(View.java:13759)
 E/AndroidRuntime(10555):   at android.view.View.getDisplayList(View.java:12710)
 E/AndroidRuntime(10555):   at android.view.View.getDisplayList(View.java:12754)
 E/AndroidRuntime(10555):   at android.view.View.draw(View.java:13483)
 E/AndroidRuntime(10555):   at android.view.ViewGroup.drawChild(ViewGroup.java:3169) 
 E/AndroidRuntime(10555):   at android.view.ViewGroup.dispatchDraw(ViewGroup.ja

Note:Faceing error this two lines

Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(),Config.ARGB_8888);  and this

    Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
3
  • 1
    stackoverflow.com/questions/477572/…. scale down your image. Commented Aug 22, 2013 at 4:47
  • 1
    Dealing with images has always been a risky business, i too have faced problems with my gaming app, You might want to look into this, if you haven't, already .developer.android.com/training/displaying-bitmaps/… Commented Aug 22, 2013 at 4:48
  • you can use inSampleSize and a worker on another thread for loading bitmap.i suggest create thumbnail Pic from your bitmap and show this thumbnail to user Commented Aug 22, 2013 at 5:30

1 Answer 1

2

I suspect that you are creating and allocating too many bitmap objects into your heap

you can optimize it using only one bitmap Object which will be reused

@Override
protected void onDraw(Canvas canvas) {

    Drawable drawable = getDrawable();

    if (drawable == null) {
        return;
    }

    if (getWidth() == 0 || getHeight() == 0) {
        return;
    }
    Bitmap bitmapCommon = ((BitmapDrawable) drawable).getBitmap();
    if (bitmapCommon == null) {
        Log.d("null", "yes");
    } else {
        Log.d("not null", "yes");
    }
     bitmapCommon = bitmapCommon.copy(Bitmap.Config.ARGB_8888, true);

    int w = getWidth(), h = getHeight();
    bitmapCommon = getCroppedBitmap(bitmapCommon, w);

    canvas.drawBitmap(bitmapCommon, 0, 0, null);

}

In the same way you can optimize getCroppedBitmap() too

Sign up to request clarification or add additional context in comments.

1 Comment

You can reduce the size of your bitmap using developer.android.com/training/displaying-bitmaps/… Also optimize getCroppedBitmap() methode too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.