http://milkmidi.blogspot.com/2012/01/javalangoutofmemoryerror-bitmap-size.html
最近在寫 Android 程式時,
一直發生
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
這個問題
後來查了一下,才知道是圖檔太大,超過能使用的記憶體
但透過
BitmapFactory.Options 這個類別就可以決解。
1
2
3
|
BitmapFactory.Options opts =
new
BitmapFactory.Options();
opts.inSampleSize =
4
;
Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);
|
其中 inSampleSize 的數值,指的是將圖片大小除以 n
重點就要是如何算出正確的數值。
官方文件有提到,該值以 2 的平方為最佳
先準備一張 3000x2000 的圖片,放到 raw 資料夾裡
再沒有使用 inSampleSize 時
直接 decode, 就會發生 OutOfMemory
要算出最佳值,要對圖片 decode 二次
第一次先得到圖片的長、寬,然後再算出 sampleSize ,
第二次再 deocde 得到所需要的圖片
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inJustDecodeBounds = true; //使用這個屬性, 就只會計算,但不會分配記憶體
Bitmap bitmap = BitmapFactory.decodeResource(res, R.raw.i3000x2000,opt);
trace(opt.outWidth,opt.outHeight);//得到原始圖片長、寬
最佳 sampleSize 算法
轉貼至:http://www.maxhis.info/androiding/bitmap-size-exceed/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
public
static
int
computeSampleSize(BitmapFactory.Options options,
int
maxNumOfPixels) {
return
computeSampleSize(options,-
1
, maxNumOfPixels);
}
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;
}
}
|
opt.inSampleSize = computeSampleSize(opt,480*800);
opt.inJustDecodeBounds=false;
trace(opt.inSampleSize);
bitmap = BitmapFactory.decodeResource(res, R.raw.i3000x2000,opt);//第二次 decode
完美。
參考文章:
http://www.maxhis.info/androiding/bitmap-size-exceed/
http://julianshen.blogspot.com/2010/08/android-bitmapoutofmemoryerror.html
http://blog.xuite.net/ffc99a3b/ooxx/44391740
http://bluegray-javalearning.blogspot.com/2011/07/android-out-of-memoryoom.html
http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue