NoviceMe
NoviceMe

Reputation: 3256

Android exception - External allocation too large for this process

I have following code:

private Bitmap  mBitmap;
private Canvas  mCanvas;
private Path    mPath;
private Paint   mBitmapPaint;


mBitmap = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);

mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);

protected void onDraw(Canvas canvas) {

canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mRealPaint);
}

When i run it, it runs fine for few pages. Then after few pictures are done it force closes giving following exception:

E/GraphicsJNI(493): VM won't let us allocate 921600 bytes
D/AndroidRuntime(493): Shutting down VM
W/dalvikvm(493): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
E/AndroidRuntime(493): FATAL EXCEPTION: main
E/AndroidRuntime(493): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

I read many posts on this topic. Updated Large heap to true, but it is still not working. Can someone please help me tell what am i doing wrong here?

More errors:

W/webcore(541): Can't get the viewWidth after the first layout
D/webviewglue(541): nativeDestroy view: 0x40a7b8
I/Ads(541): onReceiveAd()
D/dalvikvm(541): GC_EXTERNAL_ALLOC freed 1943 objects / 219448 bytes in 227ms
D/dalvikvm(541): GC_EXTERNAL_ALLOC freed 707 objects / 100576 bytes in 159ms
E/ActivityThread(541): Failed to find provider info for com.google.plus.platform

Upvotes: 0

Views: 382

Answers (1)

Yogesh Tatwal
Yogesh Tatwal

Reputation: 2751

try the following code :-

public static Bitmap createScaledBitmap(Bitmap src, int dstWidth,
        int dstHeight, boolean filter) {
    Matrix m;
    synchronized (Bitmap.class) {
        // small pool of just 1 matrix
        m = sScaleMatrix;
        sScaleMatrix = null;
    }

    if (m == null) {
        m = new Matrix();
    }

    final int width = src.getWidth();
    final int height = src.getHeight();
    final float sx = dstWidth  / (float)width;
    final float sy = dstHeight / (float)height;
    m.setScale(sx, sy);
    Bitmap b = Bitmap.createBitmap(src, 0, 0, width, height, m, filter);

    synchronized (Bitmap.class) {
        // do we need to check for null? why not just assign everytime?
        if (sScaleMatrix == null) {
            sScaleMatrix = m;
        }
    }

    return b;
}

src will be your bitmap

Upvotes: 2

Related Questions