只要在讀取時(shí)加上圖片的Config參數(shù),可以很有效減少加載的內(nèi)存,從而跟有效阻止拋out of Memory異常。
* 以最省內(nèi)存的方式讀取本地資源的圖片
* @param context
* @param resId
* @return
*/
public Bitmap readBitMap( int resId){
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
// 獲取資源圖片
InputStream is = getResources().openRawResource(resId);
return BitmapFactory.decodeStream(is, null, opt);
}```
然后在適當(dāng)?shù)臅r(shí)候及時(shí)回收?qǐng)D片占用的內(nèi)存
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}```