關於Canvas: trying to use a recycled bitmap android.graphics的疑惑

來源:互聯網
上載者:User

標籤:des   android   style   io   color   ar   使用   sp   on   

============問題描述============


因為viewpager圖片記憶體溢出的問題,不得不考慮手動釋放記憶體,不過出的問題我不理解。

我的想法是建立一個Map,然後用instantiateItem中的參數arg0當作鍵,bitmap當作值,當destroyItem中去掉VIew的時候我捎帶著把不再用到的bitmap也回收掉
//這個是存bitmap的map
public HashMap<Integer, SoftReference<Bitmap>> cacheBit;

adapter中大概的相關代碼是這樣的:

//這個是destroyItem中回收代碼
// 這裡進行回收,當我們左右滑動的時候,會把早期的圖片回收掉.
public void destroyItem(View arg0, int arg1, Object arg2) {
// TODO Auto-generated method stub
View view = (View) arg2;
((ViewPager) arg0).removeView(view);
                //回收圖片的代碼
        if (cacheBit.containsKey(arg1)) {
try {
Bitmap bm = cacheBit.get(arg1).get();
if (null != bm && !bm.isRecycled()) {
bm.recycle();
bm = null;
}
// 提醒系統回收圖片
System.gc();
} catch (Exception e) {
// TODO: handle exception
System.out.println("圖片已被回收");
}

cacheBit.remove(arg1);
}
}

//然後這個是載入view的頁面,在這個方法中我將bitmap存入map中,方便回收:
public Object instantiateItem(View arg0, int arg1) {
// TODO Auto-generated method stub
                //這一部分是不相關的代碼
View view;
LayoutInflater inflater = LayoutInflater.from(context);
view = inflater.inflate(R.layout.headingline_slider, null);
relative = (RelativeLayout) view.findViewById(R.id.bg);

                //這裡是圖片的非同步載入
// 根據圖片URL去尋找記憶體緩衝有沒有對應的Bitmap對象,並傳遞迴調方法,如果沒有,則等下載完畢回調
Bitmap bitmap = ImageLoader.loadBitmap(relative, list.get(arg1), new ImageCallBack() {
@Override
public void imageLoad(RelativeLayout relative, Bitmap bitmap) {
// TODO Auto-generated method stub
relative.setBackgroundDrawable(new BitmapDrawable(bitmap));
}
});
if (bitmap == null) {
relative.setBackgroundDrawable(new BitmapDrawable(bt));
} else {
relative.setBackgroundDrawable(new BitmapDrawable(bitmap));
}

// 將bitmap載入到map中,方便用過之後回收掉
cacheBit.put(arg1, new SoftReference<Bitmap>(bitmap));

((ViewPager) arg0).addView(view);
return view;
}



邏輯上我不知道哪裡的錯誤,但是結果執行出來滑幾次以後就會報一個異常,說我試圖使用已回收的圖片,不理解
Canvas: trying to use a recycled bitmap [email protected]

============解決方案1============


如果有相同的url  你要控製圖片的引用計數  只有為0 你才能回收。

============解決方案2============


Bitmap bitmap = ImageLoader.loadBitmap(relative, list.get(arg1), new ImageCallBack() {
@Override
public void imageLoad(RelativeLayout relative, Bitmap bitmap) {
// TODO Auto-generated method stub
relative.setBackgroundDrawable(new BitmapDrawable(bitmap));
}
});
if (bitmap == null) {
relative.setBackgroundDrawable(new BitmapDrawable(bt));
} else {
relative.setBackgroundDrawable(new BitmapDrawable(bitmap));
}

// 將bitmap載入到map中,方便用過之後回收掉
cacheBit.put(arg1, new SoftReference<Bitmap>(bitmap));

/
這裡 你有可能將bitmap=null  緩衝進cache中。
//初始化時候  你的 bitmap 必然 為null ,但是你依然將這個null 寫入緩衝



============解決方案3============


根據代碼看,你應該是使用ImageLoader 這個第三方的工具,
基本上比較完善的工具都會提供圖片緩衝機制,並有比較完善的淘汰演算法。
當你的item摧毀時候,你recyle bitmap,但是對於imageLoader 它不知這個圖片recycle了,就有可能同樣的url 他返回的還是這個recycled 的圖片。

你檢查下。 

關於Canvas: trying to use a recycled bitmap android.graphics的疑惑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.