因此,改用先通過BitmapFactory.decodeStream方法,創建出一個bitmap,再將其設爲ImageView的source,decodeStream最大的祕密在於其直接調用JNI>>nativeDecodeAsset()來完成decode,無需再使用java層的createBitmap,從而節省了java層的空間。
1. InputStream is = this.getResources().openRawResource(R.drawable.pic1);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inSampleSize = 10; //width,hight設爲原來的十分一
Bitmap btp =BitmapFactory.decodeStream(is,null,options);
2. if(!bmp.isRecycle() ){
- /**
- * 以最省內存的方式讀取本地資源的圖片
- * @param context
- * @param resId
- * @return
- */
- public static Bitmap readBitMap(Context context, int resId){
- BitmapFactory.Options opt = new BitmapFactory.Options();
- opt.inPreferredConfig = Bitmap.Config.RGB_565;
- opt.inPurgeable = true;
- opt.inInputShareable = true;
- //獲取資源圖片
- InputStream is = context.getResources().openRawResource(resId);
- return BitmapFactory.decodeStream(is,null,opt);
- }
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeStream(stream , null, opts);
iv.setImageBitmap(bitmap);
/**
* 以最省內存的方式讀取本地資源的圖片
* @param context
*@param resId
* @return
*/
public static Bitmap readBitMap(Context context, int resId){
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
//獲取資源圖片
InputStream is = context.getResources().openRawResource(resId);
return BitmapFactory.decodeStream(is,null,opt);
}
取得bitmap之後,再 detailView.setImageBitmap(pdfImage);就ok了!
那是爲什麼,會導致oom呢:
原來當使用像 imageView.setBackgroundResource,imageView.setImageResource,或者 BitmapFactory.decodeResource 這樣的方法來設置一張大圖片的時候,
這些函數在完成decode後,最終都是通過java層的createBitmap來完成的,需要消耗更多內存。
因此,改用先通過BitmapFactory.decodeStream方法,創建出一個bitmap,再將其設爲ImageView的 source,decodeStream最大的祕密在於其直接調用JNI>>nativeDecodeAsset()來完成decode,無需再使用java層的createBitmap,從而節省了java層的空間。如果在讀取時加上圖片的Config參數,可以跟有效減少加載的內存,從而跟有效阻止拋out of Memory異常。
另外,需要特別注意:
decodeStream是直接讀取圖片資料的字節碼了, 不會根據機器的各種分辨率來自動適應,使用了decodeStream之後,需要在hdpi和mdpi,ldpi中配置相應的圖片資源,否則在不同分辨率機器上都是同樣大小(像素點數量),顯示出來的大小就不對了。
1
|
BitmapFactory.decodeFile(imageFile); |
用BitmapFactory解碼一張圖片時,有時會遇到該錯誤。這往往是由於圖片過大造成的。要想正常使用,則需要分配更少的內存空間來存儲。
BitmapFactory.Options.inSampleSize
設置恰當的inSampleSize可以使BitmapFactory分配更少的空間以消除該錯誤。inSampleSize的具體含義請參考SDK文檔。例如:
1
2
3
|
BitmapFactory.Options
opts = new
BitmapFactory.Options(); opts.inSampleSize
= 4 ;
Bitmap
bitmap = BitmapFactory.decodeFile(imageFile, opts); |
如何設置恰當的inSampleSize
設置恰當的inSampleSize是解決該問題的關鍵之一。BitmapFactory.Options提供了另一個成員inJustDecodeBounds。
1
2
3
|
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提供了一種動態計算的方法。
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
|
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。
1
2
3
4
5
6
7
8
9
10
11
|
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()方法來釋放位圖所佔的空間,當然前提是位圖沒有被使用。