Android异常 - 此进程的外部分配太大

时间:2013-06-29 18:55:56

标签: android eclipse

我有以下代码:

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);
}

当我运行它时,它可以运行几页。然后在完成几张照片后,它会关闭以下异常:

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

我读了很多关于这个主题的帖子。将大堆更新为true,但它仍然无法正常工作。有人可以帮我告诉我这里做错了什么吗?

更多错误:

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

1 个答案:

答案 0 :(得分:2)

尝试以下代码: -

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将是您的位图