android java.lang.OutOfMemoryError: bitmap size exceeds VM budget

當圖片過大,或圖片數量較多時使用BitmapFactory解碼圖片會出java.lang.OutOfMemoryError: bitmap size exceeds VM budget,要想正常使用則需分配更少的內存,具體的解決辦法是修改采樣值BitmapFactory.Options.inSampleSize,例如:www.aiwalls.com

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);如何設置恰當的inSampleSize

設置恰當的inSampleSize是解決該問題的關鍵之一。BitmapFactory.Options提供瞭另一個成員inJustDecodeBounds。

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);

設置inJustDecodeBounds為true後,decodeFile並不分配空間,但可計算出原始圖片的長度和寬度,即opts.width和opts.height。有瞭這兩個參數,再通過一定的算法,即可得到一個恰當的inSampleSize。

查看Android源碼,Android提供瞭一種動態計算的方法。

public static int computeSampleSize(BitmapFactory.Options options,
        int minSideLength, int maxNumOfPixels) {
    int initialSize = computeInitialSampleSize(options, minSideLength,maxNumOfPixels);

    int roundedSize;
    if (initialSize <= 8 ) {
        roundedSize = 1;
        while (roundedSize < initialSize) {
            roundedSize <<= 1;
        }
    } else {
        roundedSize = (initialSize + 7) / 8 * 8;
    }

    return roundedSize;
}

private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {
    double w = options.outWidth;
    double h = options.outHeight;

    int lowerBound = (maxNumOfPixels == -1) ? 1 :
            (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
    int upperBound = (minSideLength == -1) ? 128 :
            (int) Math.min(Math.floor(w / minSideLength),
            Math.floor(h / minSideLength));

    if (upperBound < lowerBound) {
        // return the larger one when there is no overlapping zone.
        return lowerBound;
    }

    if ((maxNumOfPixels == -1) &&
            (minSideLength == -1)) {
        return 1;
    } else if (minSideLength == -1) {
        return lowerBound;
    } else {
        return upperBound;
    }
}

使用該算法,就可動態計算出圖片的inSampleSize。

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imageFile, opts);

opts.inSampleSize = computeSampleSize(opts, -1, 128*128);
opts.inJustDecodeBounds = false;
try {
 Bitmap bmp = BitmapFactory.decodeFile(imageFile, opts);
 imageView.setImageBitmap(bmp);
    } catch (OutOfMemoryError err) {
}

另外,可以通過Bitmap.recycle()方法來釋放位圖所占的空間,當然前提是位圖沒有被使用。

 

作者:LuoXianXion

發佈留言