转载 Android解决java.lang.OutOfMemoryError: bitmap size exceeds VM budget
生活随笔
收集整理的這篇文章主要介紹了
转载 Android解决java.lang.OutOfMemoryError: bitmap size exceeds VM budget
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
當(dāng)圖片過(guò)大,或圖片數(shù)量較多時(shí)使用BitmapFactory解碼圖片會(huì)出java.lang.OutOfMemoryError: bitmap size exceeds VM budget,要想正常使用則需分配更少的內(nèi)存,具體的解決辦法是修改采樣值BitmapFactory.Options.inSampleSize,例如:
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);
如何設(shè)置恰當(dāng)?shù)膇nSampleSize
設(shè)置恰當(dāng)?shù)膇nSampleSize是解決該問(wèn)題的關(guān)鍵之一。BitmapFactory.Options提供了另一個(gè)成員inJustDecodeBounds。
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);
設(shè)置inJustDecodeBounds為true后,decodeFile并不分配空間,但可計(jì)算出原始圖片的長(zhǎng)度和寬度,即opts.width和opts.height。有了這兩個(gè)參數(shù),再通過(guò)一定的算法,即可得到一個(gè)恰當(dāng)?shù)膇nSampleSize。
查看Android源碼,Android提供了一種動(dòng)態(tài)計(jì)算的方法。
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;
}
}
使用該算法,就可動(dòng)態(tài)計(jì)算出圖片的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) {
}
另外,可以通過(guò)Bitmap.recycle()方法來(lái)釋放位圖所占的空間,當(dāng)然前提是位圖沒(méi)有被使用。
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);
如何設(shè)置恰當(dāng)?shù)膇nSampleSize
設(shè)置恰當(dāng)?shù)膇nSampleSize是解決該問(wèn)題的關(guān)鍵之一。BitmapFactory.Options提供了另一個(gè)成員inJustDecodeBounds。
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);
設(shè)置inJustDecodeBounds為true后,decodeFile并不分配空間,但可計(jì)算出原始圖片的長(zhǎng)度和寬度,即opts.width和opts.height。有了這兩個(gè)參數(shù),再通過(guò)一定的算法,即可得到一個(gè)恰當(dāng)?shù)膇nSampleSize。
查看Android源碼,Android提供了一種動(dòng)態(tài)計(jì)算的方法。
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;
}
}
使用該算法,就可動(dòng)態(tài)計(jì)算出圖片的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) {
}
另外,可以通過(guò)Bitmap.recycle()方法來(lái)釋放位圖所占的空間,當(dāng)然前提是位圖沒(méi)有被使用。
轉(zhuǎn)載于:https://www.cnblogs.com/xiao0/archive/2011/09/13/2174399.html
總結(jié)
以上是生活随笔為你收集整理的转载 Android解决java.lang.OutOfMemoryError: bitmap size exceeds VM budget的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: poj3160
- 下一篇: android sina oauth2.