android加載大量圖片內存溢出的三種解決辦法

方法一: html

在從網絡或本地加載圖片的時候,只加載縮略圖。 緩存


/**
  1. * 按照路徑加載圖片
  2. * @param path 圖片資源的存放路徑
  3. * @param scalSize 縮小的倍數
  4. * @return
  5. */
  6. public static Bitmap loadResBitmap(String path, int scalSize) {
  7. BitmapFactory.Options options = new BitmapFactory.Options();
  8. options.inJustDecodeBounds = false;
  9. options.inSampleSize = scalSize;
  10. Bitmap bmp = BitmapFactory.decodeFile(path, options);
  11. return bmp;
  12. }

這個方法的確可以少佔用很多內存,但是它的致命的缺點就是,由於加載的是縮略圖,因此圖片失真比較嚴重,對於對圖片質量要求很高的應用,能夠採用下面的方法。 網絡

方法二: 工具

運用JAVA的軟引用,進行圖片緩存,將常常須要加載的圖片,存放在緩存裏,避免反覆加載。 this

關於軟引用(SoftReference)的詳細說明,請參看http://www.auyou.cn/club/clubbbsinfo-9255.html。下面是我寫的一個圖片緩存的工具類。 spa

/**
  1. *
  2. * @author larson.liu
  3. * 該類用於圖片緩存,防止內存溢出
  4. */
  5. public class BitmapCache {
  6. static * BitmapCache cache;
  7. /** 用於Chche內容的存儲*/
  8. * Hashtable bitmapRefs;
  9. /** 垃圾Reference的隊列(所引用的對象已經被回收,則將該引用存入隊列中)*/
  10. * ReferenceQueue q;

  11. /**
  12. * 繼承SoftReference,使得每個實例都具備可識別的標識。
  13. */
  14. * class BtimapRef extends SoftReference {
  15. * Integer _key = 0;

  16. public BtimapRef(Bitmap bmp, ReferenceQueue q, int key) {
  17. super(bmp, q);
  18. _key = key;
  19. }
  20. }

  21. * BitmapCache() {
  22. bitmapRefs = new Hashtable();
  23. q = new ReferenceQueue();

  24. }

  25. /**
  26. * 取得緩存器實例
  27. */
  28. public static BitmapCache getInstance() {
  29. if (cache == null) {
  30. cache = new BitmapCache();
  31. }
  32. return cache;

  33. }

  34. /**
  35. * 以軟引用的方式對一個Bitmap對象的實例進行引用並保存該引用
  36. */
  37. * void addCacheBitmap(Bitmap bmp, Integer key) {
  38. cleanCache();// 清除垃圾引用
  39. BtimapRef ref = new BtimapRef(bmp, q, key);
  40. bitmapRefs.put(key, ref);
  41. }

  42. /**
  43. * 依據所指定的drawable下的圖片資源ID號(能夠根據本身的須要從網絡或本地path下獲取),從新獲取相應Bitmap對象的實例
  44. */
  45. public Bitmap getBitmap(int resId, Context context) {
  46. Bitmap bmp = null;
  47. // 緩存中是否有該Bitmap實例的軟引用,若是有,從軟引用中取得。
  48. if (bitmapRefs.containsKey(resId)) {
  49. BtimapRef ref = (BtimapRef) bitmapRefs.get(resId);
  50. bmp = (Bitmap) ref.get();
  51. }
  52. // 若是沒有軟引用,或者從軟引用中獲得的實例是null,從新構建一個實例,
  53. // 並保存對這個新建實例的軟引用
  54. if (bmp == null) {
  55. bmp = BitmapFactory.decodeResource(context.getResources(), resId);
  56. this.addCacheBitmap(bmp, resId);
  57. }
  58. return bmp;
  59. }

  60. * void cleanCache() {
  61. BtimapRef ref = null;
  62. while ((ref = (BtimapRef) q.poll()) != null) {
  63. bitmapRefs.remove(ref._key);
  64. }
  65. }

  66. // 清除Cache內的所有內容
  67. public void clearCache() {
  68. cleanCache();
  69. bitmapRefs.clear();
  70. System.gc();
  71. System.runFinalization();
  72. }

  73. }

在程序代碼中調用該類: .net

imageView.setImageBitmap(bmpCache.getBitmap(R.drawable.kind01, this)); code

這樣當你的imageView須要來回變換背景圖片時,就不須要再重複加載。 htm

方法三: 對象

及時銷燬再也不使用的Bitmap對象。

if (bitmap != null && b!itmap.isRecycled()){

bitmap.recycle();

bitmap = null; // recycle()是個比較漫長的過程,設爲null,而後在最後調用System.gc(),效果能好不少

}

System.gc();

相關文章
相關標籤/搜索