android中图片的三级cache策略(内存、文件、网络)之三:文件缓存策略

原文链接:http://blog.csdn.net/singwhatiwanna/article/details/17588159


前言

如果想很好地理解下面的故事,请参看我半年前写的两篇博文:android中图片的三级cache策略(内存、文件、网络) 一 和 android中左右滑屏的实现(广告位banner组件),还有昨天晚上写的android中图片的三级cache策略(内存、文件、网络)之二:内存缓存策略

文件缓存策略

当一张图片从网络下载成功以后,这个图片会被加入内存缓存和文件缓存,对于文件缓存来说,这张图片将被以url的哈希值加cach后缀名的形式存储在SD卡上,这样,当下一次再需要同一个url的图片的时候,就不需要从网络下载了,而是直接通过url来进行查找。同时一张图片被访问时,它的最后修改时间将被更新,这样的意义在于:当SD卡空间不足的时候,将会按照最后修改时间来删除40%缓存的图片,确切来说,那些修改时间比较早的图片将会被删除。

代码展示

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class ImageFileCache  
  2. {  
  3.     private static final String TAG = "ImageFileCache";  
  4.       
  5.     //图片缓存目录  
  6.     private static final String IMGCACHDIR = "/sdcard/ImgCach";  
  7.       
  8.     //保存的cache文件宽展名  
  9.     private static final String CACHETAIL = ".cach";  
  10.                                                               
  11.     private static final int MB = 1024*1024;  
  12.       
  13.     private static final int CACHE_SIZE = 1;  
  14.       
  15.     //当SD卡剩余空间小于10M的时候会清理缓存  
  16.     private static final int FREE_SD_SPACE_NEEDED_TO_CACHE = 10;  
  17.                                                                   
  18.     public ImageFileCache()   
  19.     {  
  20.         //清理部分文件缓存  
  21.         removeCache(IMGCACHDIR);              
  22.     }  
  23.                                                                   
  24.     /**  
  25.      * 从缓存中获取图片  
  26.      */  
  27.     public Bitmap getImageFromFile(final String url)   
  28.     {      
  29.         final String path = IMGCACHDIR + "/" + convertUrlToFileName(url);  
  30.         File file = new File(path);  
  31.         if (file != null && file.exists())   
  32.         {  
  33.             Bitmap bmp = BitmapFactory.decodeFile(path);  
  34.             if (bmp == null)   
  35.             {  
  36.                 file.delete();  
  37.             }   
  38.             else   
  39.             {  
  40.                 updateFileTime(path);  
  41.                 Logger.d(TAG, "get bmp from FileCache,url=" + url);  
  42.                 return bmp;  
  43.             }  
  44.         }  
  45.         return null;  
  46.     }  
  47.                                                                   
  48.     /** 
  49.      * 将图片存入文件缓存  
  50.      */  
  51.     public void saveBitmapToFile(Bitmap bm, String url)   
  52.     {  
  53.         if (bm == null) {  
  54.             return;  
  55.         }  
  56.         //判断sdcard上的空间  
  57.         if (FREE_SD_SPACE_NEEDED_TO_CACHE > SdCardFreeSpace())   
  58.         {  
  59.             //SD空间不足  
  60.             return;  
  61.         }  
  62.           
  63.         String filename = convertUrlToFileName(url);  
  64.         File dirFile = new File(IMGCACHDIR);  
  65.         if (!dirFile.exists())  
  66.             dirFile.mkdirs();  
  67.         File file = new File(IMGCACHDIR +"/" + filename);  
  68.         try   
  69.         {  
  70.             file.createNewFile();  
  71.             OutputStream outStream = new FileOutputStream(file);  
  72.             bm.compress(Bitmap.CompressFormat.JPEG, 100, outStream);  
  73.             outStream.flush();  
  74.             outStream.close();  
  75.         }   
  76.         catch (FileNotFoundException e)   
  77.         {  
  78.             Logger.d(TAG, "FileNotFoundException");  
  79.         }   
  80.         catch (IOException e)   
  81.         {  
  82.             Logger.d(TAG, "IOException");  
  83.         }  
  84.     }   
  85.                                                                   
  86.     /** 
  87.      * 计算存储目录下的文件大小, 
  88.      * 当文件总大小大于规定的CACHE_SIZE或者sdcard剩余空间小于FREE_SD_SPACE_NEEDED_TO_CACHE的规定 
  89.      * 那么删除40%最近没有被使用的文件 
  90.      */  
  91.     private boolean removeCache(String dirPath)   
  92.     {  
  93.         File dir = new File(dirPath);  
  94.         File[] files = dir.listFiles();  
  95.           
  96.         if (files == null)   
  97.         {  
  98.             return true;  
  99.         }  
  100.           
  101.         if (!android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))  
  102.         {  
  103.             return false;  
  104.         }  
  105.                                                               
  106.         int dirSize = 0;  
  107.         for (int i = 0; i < files.length; i++)   
  108.         {  
  109.             if (files[i].getName().contains(CACHETAIL))   
  110.             {  
  111.                 dirSize += files[i].length();  
  112.             }  
  113.         }  
  114.                                                               
  115.         if (dirSize > CACHE_SIZE * MB || FREE_SD_SPACE_NEEDED_TO_CACHE > SdCardFreeSpace())   
  116.         {  
  117.             int removeFactor = (int) (0.4 * files.length);  
  118.             Arrays.sort(files, new FileLastModifSort());  
  119.             for (int i = 0; i < removeFactor; i++)   
  120.             {  
  121.                 if (files[i].getName().contains(CACHETAIL))   
  122.                 {  
  123.                     files[i].delete();  
  124.                 }  
  125.             }  
  126.         }  
  127.                                                               
  128.         if (SdCardFreeSpace() <= CACHE_SIZE)   
  129.         {  
  130.             return false;  
  131.         }  
  132.                                                                       
  133.         return true;  
  134.     }  
  135.                                                                   
  136.     /** 
  137.      * 修改文件的最后修改时间 
  138.      */  
  139.     public void updateFileTime(String path)   
  140.     {  
  141.         File file = new File(path);  
  142.         long newModifiedTime = System.currentTimeMillis();  
  143.         file.setLastModified(newModifiedTime);  
  144.     }  
  145.                                                                   
  146.     /**  
  147.      * 计算SD卡上的剩余空间  
  148.      */  
  149.     private int SdCardFreeSpace()  
  150.     {  
  151.         StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());  
  152.         double sdFreeMB = ((double)stat.getAvailableBlocks() * (double) stat.getBlockSize()) / MB;  
  153.         return (int) sdFreeMB;  
  154.     }   
  155.                                                                   
  156.     /**  
  157.      * 将url转成文件名  
  158.      */  
  159.     private String convertUrlToFileName(String url)  
  160.     {  
  161.         return url.hashCode() + CACHETAIL;  
  162.     }  
  163.                                                                   
  164.     /** 
  165.      * 根据文件的最后修改时间进行排序 
  166.      */  
  167.     private class FileLastModifSort implements Comparator<File>   
  168.     {  
  169.         public int compare(File file0, File file1)   
  170.         {  
  171.             if (file0.lastModified() > file1.lastModified())  
  172.             {  
  173.                 return 1;  
  174.             }   
  175.             else if (file0.lastModified() == file1.lastModified())   
  176.             {  
  177.                 return 0;  
  178.             }   
  179.             else   
  180.             {  
  181.                 return -1;  
  182.             }  
  183.         }  
  184.     }  
  185.   
  186. }  

说明:上述代码很好理解而且都有注释,不用多说了。


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值