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);
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98
  • 1
    http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object. scale down your image. – Raghunandan Aug 22 '13 at 04: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 .http://developer.android.com/training/displaying-bitmaps/load-bitmap.html– – stack_ved Aug 22 '13 at 04: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 – Saeed-rz Aug 22 '13 at 05:30

1 Answers1

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

Arun C
  • 9,035
  • 2
  • 28
  • 42
  • You can reduce the size of your bitmap using http://developer.android.com/training/displaying-bitmaps/load-bitmap.html Also optimize getCroppedBitmap() methode too – Arun C Aug 22 '13 at 10:29