Android bitmap size exceeds VM budget.

My app is getting this error frequently. I have two questions.

  1. Do I need to recycle my about activity (it contains some imageviews and buttons and textViews)?
  2. What is the difference between .recycle(); and between system.gc(); ?
有帮助吗?

解决方案

You should always try and recycle Bitmaps afte you have used them.

As far as I understand, you should try and avoid calling system.gc(). Calling recycle() will allow the bitmap object to be garbage collected.

I hope this helps.

其他提示

Try watching this video (romain guy):

http://www.youtube.com/watch?v=duefsFTJXzc&list=PLD1B287286E23E2D1&index=1&feature=plpp_video

It will provide some insight into best practices for Bitmaps.

I got the same problem while picking images from camera.
I resized the bitmap of image using following code:

Bitmap bitmap = resizeBitMapImage(picturePath, 75, 91);
            profilePic.setImageBitmap(bitmap);

private Bitmap resizeBitMapImage(String filePath, int targetWidth,
        int targetHeight) {

    Bitmap bitMapImage = null;
    // First, get the dimensions of the image
    Options options = new Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);
    double sampleSize = 0;
    // Only scale if we need to
    // (16384 buffer for img processing)
    Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math
            .abs(options.outWidth - targetWidth);

    if (options.outHeight * options.outWidth * 2 >= 1638) {
        // Load, scaling to smallest power of 2 that'll get it <= desired
        // dimensions
        sampleSize = scaleByHeight ? options.outHeight / targetHeight
                : options.outWidth / targetWidth;
        sampleSize = (int) Math.pow(2d,
                Math.floor(Math.log(sampleSize) / Math.log(2d)));
    }

    // Do the actual decoding
    options.inJustDecodeBounds = false;
    options.inTempStorage = new byte[128];
    while (true) {
        try {
            options.inSampleSize = (int) sampleSize;
            bitMapImage = BitmapFactory.decodeFile(filePath, options);

            break;
        } catch (Exception ex) {
            try {
                sampleSize = sampleSize * 2;
            } catch (Exception ex1) {

            }
        }
    }
     return bitMapImage;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top