Android 遠程圖片擷取和本機快取(二)

來源:互聯網
上載者:User

  修改檔案的最後修改時間

java代碼:

  1. /**
  2. * 修改檔案的最後修改時間
  3. * @param dir
  4. * @param fileName
  5. */
  6. private void updateFileTime(String dir,String fileName) {
  7. File file = new File(dir,fileName); 
  8. long newModifiedTime =System.currentTimeMillis();
  9. file.setLastModified(newModifiedTime);
  10. }

複製代碼

       本機快取最佳化

java代碼:

  1. /**
  2. *計算儲存目錄下的檔案大小,當檔案總大小大於規定的CACHE_SIZE或者sdcard剩餘空間小於FREE_SD_SPACE_NEEDED_TO_CACHE的規定
  3. * 那麼刪除40%最近沒有被使用的檔案
  4. * @param dirPath
  5. * @param filename
  6. */
  7. private void removeCache(String dirPath) {
  8. File dir = new File(dirPath);
  9. File[] files = dir.listFiles();
  10. if (files == null) {
  11. return;
  12. }
  13. int dirSize = 0;
  14. for (int i = 0; i < files.length;i++) {
  15. if(files[i].getName().contains(WHOLESALE_CONV)) {
  16. dirSize += files[i].length();
  17. }
  18. }
  19. if (dirSize > CACHE_SIZE * MB ||FREE_SD_SPACE_NEEDED_TO_CACHE > freeSpaceOnSd()) {
  20. int removeFactor = (int) ((0.4 *files.length) + 1);
  21. Arrays.sort(files, newFileLastModifSort());
  22. Log.i(TAG, "Clear some expiredcache files ");
  23. for (int i = 0; i <removeFactor; i++) {
  24. if(files[i].getName().contains(WHOLESALE_CONV)) {
  25. files[i].delete(); 
  26. }
  27. }
  28. }
  29. }
  30. /**
  31. * 刪除到期檔案
  32. * @param dirPath
  33. * @param filename
  34. */
  35. private void removeExpiredCache(StringdirPath, String filename) {
  36. File file = new File(dirPath,filename);
  37. if (System.currentTimeMillis() -file.lastModified() > mTimeDiff) {
  38. Log.i(TAG, "Clear some expiredcache files ");
  39. file.delete();
  40. }
  41. }

複製代碼

       檔案使用時間排序

java代碼:

  1. /**
  2. * TODO 根據檔案的最後修改時間進行排序 *
  3. */
  4. classFileLastModifSort implements Comparator<File>{
  5. public int compare(File arg0, File arg1) {
  6. if (arg0.lastModified() >arg1.lastModified()) {
  7. return 1;
  8. } else if (arg0.lastModified() ==arg1.lastModified()) {
  9. return 0;
  10. } else {
  11. return -1;
  12. }
  13. }
  14. }

複製代碼

       記憶體儲存:
       在記憶體中儲存的話,只能儲存一定的量,而不能一直往裡面放,需要設定資料的到期時間、LRU等演算法。這裡有一個方法是把常用的資料放到一個緩衝中(A),不常用的放到另外一個緩衝中(B)。當要擷取資料時先從A中去擷取,如果A中不存在那麼再去B中擷取。B中的資料主要是A中LRU出來的資料,這裡的記憶體回收主要針對B記憶體,從而保持A中的資料可以有效被命中。

 

       先定義A緩衝:

 

java代碼:

  1. private final HashMap<String, Bitmap>mHardBitmapCache = new LinkedHashMap<String, Bitmap>(HARD_CACHE_CAPACITY/ 2, 0.75f, true) {
  2. @Override
  3. protected booleanremoveEldestEntry(LinkedHashMap.Entry<String, Bitmap> eldest) {
  4. if (size() >HARD_CACHE_CAPACITY) {
  5. //當map的size大於30時,把最近不常用的key放到mSoftBitmapCache中,從而保證mHardBitmapCache的效率
  6. mSoftBitmapCache.put(eldest.getKey(), newSoftReference<Bitmap>(eldest.getValue()));
  7. return true;
  8. } else
  9. return false;
  10. }
  11. };

複製代碼

       再定於B緩衝:

java代碼:

  1. /**
  2. *當mHardBitmapCache的key大於30的時候,會根據LRU演算法把最近沒有被使用的key放入到這個緩衝中。
  3. *Bitmap使用了SoftReference,當記憶體空間不足時,此cache中的bitmap會被記憶體回收掉
  4. */
  5. private final staticConcurrentHashMap<String, SoftReference<Bitmap>> mSoftBitmapCache =new ConcurrentHashMap

複製代碼

系列之Android 遠程圖片擷取和本機快取(一)的文章連結http://www.eoeandroid.com/thread-98446-1-1.html
系列之Android 遠程圖片擷取和本機快取(三)的文章連結http://www.eoeandroid.com/thread-98450-1-1.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.