在編寫Android程序的時候,我們總是難免會碰到OOM(OUT OF MEMORY)的錯誤,那麼這個錯誤究竟是怎麼來的呢,可以先看一下這篇文章ANDROID BITMAP內存限制OOM,OUT OF MEMORY。
這裏,我使用Gallery來舉例,在模擬器中,不會出現OOM錯誤,但是,一旦把程序運行到真機裏,圖片文件一多,必然會出現OOM,我們通過做一些額外的處理來避免。
1.創建一個圖片緩存對象HashMap<Integer,Bitmap> dataCache,integer對應Adapter中的位置position,我們只用緩存處在顯示中的圖片,對於之外的位置,如果dataCache中有對應的圖片,我們需要進行回收內存。在這個例子中,Adapter對象的getView方法首先判斷該位置是否有緩存的bitmap,如果沒有,則解碼圖片(bitmapDecoder.getPhotoItem,BitmapDecoder類見後面)並返回bitmap對象,設置dataCache在該位置上的bitmap緩存以便之後使用;若是該位置存在緩存,則直接取出來使用,避免了再一次調用底層的解碼圖像需要的內存開銷。有時爲了提高Gallery的更新速度,我們還可以預存儲一些位置上的bitmap,比如存儲顯示區域位置外向上3個向下3個位置的bitmap,這樣上或下滾動Gallery時可以加快getView的獲取。
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.photo_item, null);
holder = new ViewHolder();
holder.photo = (ImageView) convertView.findViewById(R.id.photo_item_image);
holder.photoTitle = (TextView) convertView.findViewById(R.id.photo_item_title);
holder.photoDate = (TextView) convertView.findViewById(R.id.photo_item_date);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
cursor.moveToPosition(position);
Bitmap current = dateCache.get(position);
if(current != null){//如果緩存中已解碼該圖片,則直接返回緩存中的圖片
holder.photo.setImageBitmap(current);
}else {
current = bitmapDecoder.getPhotoItem(cursor.getString(1), 2) ;
holder.photo.setImageBitmap(current);
dateCache.put(position, current);
}
holder.photoTitle.setText(cursor.getString(2));
holder.photoDate.setText(cursor.getString(4));
return convertView;
}
}
package com.wuyi.bestjoy;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
public class BitmapDecoder {
private static final String TAG = "BitmapDecoder";
private Context context;
public BitmapDecoder(Context context) {
this.context = context;
}
public Bitmap getPhotoItem(String filepath,int size) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=size;
Bitmap bitmap = BitmapFactory.decodeFile(filepath,options);
bitmap=Bitmap.createScaledBitmap(bitmap, 180, 251, true);//預先縮放,避免實時縮放,可以提高更新率
return bitmap;
}
}
2.由於Gallery控件的特點,總有一個item處於當前選擇狀態,我們利用此時進行dataCache中額外不用的bitmap的清理,來釋放內存。
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
releaseBitmap();
Log.v(TAG, "select id:"+ id);
}
private void releaseBitmap(){
//在這,我們分別預存儲了第一個和最後一個可見位置之外的3個位置的bitmap
//即dataCache中始終只緩存了(M=6+Gallery當前可見view的個數)M個bitmap
int start = mGallery.getFirstVisiblePosition()-3;
int end = mGallery.getLastVisiblePosition()+3;
Log.v(TAG, "start:"+ start);
Log.v(TAG, "end:"+ end);
//釋放position<start之外的bitmap資源
Bitmap delBitmap;
for(int del=0;del<start;del++){
delBitmap = dateCache.get(del);
if(delBitmap != null){
//如果非空則表示有緩存的bitmap,需要清理
Log.v(TAG, "release position:"+ del);
//從緩存中移除該del->bitmap的映射
dateCache.remove(del);
delBitmap.recycle();
}
}
freeBitmapFromIndex(end);
}
/**
* 從某一位置開始釋放bitmap資源
* @param index
*/
private void freeBitmapFromIndex(int end) {
//釋放之外的bitmap資源
Bitmap delBitmap;
for(int del =end+1;del<dateCache.size();del++){
delBitmap = dateCache.get(del);
if(delBitmap != null){
dateCache.remove(del);
delBitmap.recycle();
Log.v(TAG, "release position:"+ del);
}
}
}
經過這些額外的操作,有效的避免了OOM的問題。