zoukankan      html  css  js  c++  java
  • 获取缓存大小和清除缓存功能

    转载:http://blog.csdn.net/wwj_748/article/details/42737607

      1 /**
      2  * 主要功能有清除内/外缓存,清除数据库,清除sharedPreference,清除files和清除自定义目录
      3  */
      4 import android.content.Context;
      5 import android.os.Environment;
      6 import android.text.TextUtils;
      7 import java.io.File;
      8 import java.math.BigDecimal;
      9 
     10 
     11 /** * 本应用数据清除管理器 */
     12 public class DataCleanManager {
     13     /**
     14      * * 清除本应用内部缓存(/data/data/com.xxx.xxx/cache) * * 16      * @param context
     17      */
     18     public static void cleanInternalCache(Context context) {
     19         deleteFilesByDirectory(context.getCacheDir());
     20     }
     21 
     22     /**
     23      * * 清除本应用所有数据库(/data/data/com.xxx.xxx/databases) * * 25      * @param context
     26      */
     27     public static void cleanDatabases(Context context) {
     28         deleteFilesByDirectory(new File("/data/data/"
     29                 + context.getPackageName() + "/databases"));
     30     }
     31 
     32     /**
     33      * * 清除本应用SharedPreference(/data/data/com.xxx.xxx/shared_prefs) *
     34      *
     35      * @param context
     36      */
     37     public static void cleanSharedPreference(Context context) {
     38         deleteFilesByDirectory(new File("/data/data/"
     39                 + context.getPackageName() + "/shared_prefs"));
     40     }
     41 
     42     /**
     43      * * 按名字清除本应用数据库 * * 45      * @param context
     46      * @param dbName
     47      */
     48     public static void cleanDatabaseByName(Context context, String dbName) {
     49         context.deleteDatabase(dbName);
     50     }
     51 
     52     /**
     53      * * 清除/data/data/com.xxx.xxx/files下的内容 * * 55      * @param context
     56      */
     57     public static void cleanFiles(Context context) {
     58         deleteFilesByDirectory(context.getFilesDir());
     59     }
     60 
     61     /**
     62      * * 清除外部cache下的内容(/mnt/sdcard/android/data/com.xxx.xxx/cache) 64      * @param context
     65      */
     66     public static void cleanExternalCache(Context context) {
     67         if (Environment.getExternalStorageState().equals(
     68                 Environment.MEDIA_MOUNTED)) {
     69             deleteFilesByDirectory(context.getExternalCacheDir());
     70         }
     71     }
     72     /**
     73      * * 清除自定义路径下的文件,使用需小心,请不要误删。而且只支持目录下的文件删除 * * 75      * @param filePath
     76      * */
     77     public static void cleanCustomCache(String filePath) {
     78         deleteFilesByDirectory(new File(filePath));
     79     }
     80 
     81     /**
     82      * * 清除本应用所有的数据 * * 84      * @param context
     85      * @param filepath
     86      */
     87     public static void cleanApplicationData(Context context, String... filepath) {
     88         cleanInternalCache(context);
     89         cleanExternalCache(context);
     90         cleanDatabases(context);
     91         cleanSharedPreference(context);
     92         cleanFiles(context);
     93         if (filepath == null) {
     94             return;
     95         }
     96         for (String filePath : filepath) {
     97             cleanCustomCache(filePath);
     98         }
     99     }
    100 
    101     /**
    102      * * 删除方法 这里只会删除某个文件夹下的文件,如果传入的directory是个文件,将不做处理 * *104      * @param directory
    105      */
    106     private static void deleteFilesByDirectory(File directory) {
    107         if (directory != null && directory.exists() && directory.isDirectory()) {
    108             for (File item : directory.listFiles()) {
    109                 item.delete();
    110             }
    111         }
    112     }
    113 
    114     // 获取文件
    115   //Context.getExternalFilesDir() --> SDCard/Android/data/你的应用的包名/files/ 目录,一般放一些长时间保存的数据
    116   //Context.getExternalCacheDir() --> SDCard/Android/data/你的应用包名/cache/目录,一般存放临时缓存数据
    117     public static long getFolderSize(File file) throws Exception {
    118         long size = 0;
    119         try {
    120             File[] fileList = file.listFiles();
    121             for (int i = 0; i < fileList.length; i++) {
    122                 // 如果下面还有文件
    123                 if (fileList[i].isDirectory()) {
    124                     size = size + getFolderSize(fileList[i]);
    125                 } else {
    126                     size = size + fileList[i].length();
    127                 }
    128             }
    129         } catch (Exception e) {
    130             e.printStackTrace();
    131         }
    132         return size;
    133     }
    134 
    135     /**
    136      * 删除指定目录下文件及目录
    137      *
    138      * @param filePath
    139      * @param deleteThisPath
    140      * @return
    141      */
    142     public static void deleteFolderFile(String filePath, boolean deleteThisPath) {
    143         if (!TextUtils.isEmpty(filePath)) {
    144             try {
    145                 File file = new File(filePath);
    146                 if (file.isDirectory()) {// 如果下面还有文件
    147                     File files[] = file.listFiles();
    148                     for (int i = 0; i < files.length; i++) {
    149                         deleteFolderFile(files[i].getAbsolutePath(), true);
    150                     }
    151                 }
    152                 if (deleteThisPath) {
    153                     if (!file.isDirectory()) {// 如果是文件,删除
    154                         file.delete();
    155                     } else {// 目录
    156                         if (file.listFiles().length == 0) {// 目录下没有文件或者目录,删除
    157                             file.delete();
    158                         }
    159                     }
    160                 }
    161             } catch (Exception e) {
    162                 // TODO Auto-generated catch block
    163                 e.printStackTrace();
    164             }
    165         }
    166     }
    167 
    168     /**
    169      * 格式化单位
    170      *
    171      * @param size
    172      * @return
    173      */
    174     public static String getFormatSize(double size) {
    175         double kiloByte = size / 1024;
    176         if (kiloByte < 1) {
    177             return size + "Byte";
    178         }
    179 
    180         double megaByte = kiloByte / 1024;
    181         if (megaByte < 1) {
    182             BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
    183             return result1.setScale(2, BigDecimal.ROUND_HALF_UP)
    184                     .toPlainString() + "KB";
    185         }
    186 
    187         double gigaByte = megaByte / 1024;
    188         if (gigaByte < 1) {
    189             BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
    190             return result2.setScale(2, BigDecimal.ROUND_HALF_UP)
    191                     .toPlainString() + "MB";
    192         }
    193 
    194         double teraBytes = gigaByte / 1024;
    195         if (teraBytes < 1) {
    196             BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
    197             return result3.setScale(2, BigDecimal.ROUND_HALF_UP)
    198                     .toPlainString() + "GB";
    199         }
    200         BigDecimal result4 = new BigDecimal(teraBytes);
    201         return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()
    202                 + "TB";
    203     }
    204 
    205 
    206     public static String getCacheSize(File file) throws Exception {
    207         return getFormatSize(getFolderSize(file));
    208     }
    209 
    210 }
     1        try {
     2             Log.i("tag", "缓存getCacheDir.." + DataCleanManager.getCacheSize(mActivity.getCacheDir()));
     3             Log.i("tag", "缓存getExternalCacheDir.."
     4                     + DataCleanManager.getCacheSize(mActivity.getExternalCacheDir()));
     5             String str1 = "/data/data/com.coach.app/cache"; //- 应用内缓存(注:对应方法getCacheDir())
     6             String str2 = "/data/data/com.coach.app/databases"; // - 应用内数据库
     7             String str3 = "/data/data/com.coach.app/shared_prefs";  //- 应用内配置文件
     8             String str4 = "/data/data/com.coach.app/files";  //- 应用内文件(注:对应方法getFilesDir())
     9             Log.i("tag", "应用内缓存..." + DataCleanManager.getCacheSize(new File(str1)) );
    10             Log.i("tag", "应用内数据库..." + DataCleanManager.getCacheSize(new File(str2)) );
    11             Log.i("tag", "应用内配置文件..." + DataCleanManager.getCacheSize(new File(str3)) );
    12             Log.i("tag", "应用内文件getFilesDir()..." + DataCleanManager.getCacheSize(new File(str4)) );
    13         } catch (Exception e) {
    14             e.printStackTrace();
    15         }

    缓存getCacheDir..26.98KB
    缓存getExternalCacheDir..31.0Byte
    应用内缓存...26.98KB
    应用内数据库...28.52KB
    应用内配置文件...2.82KB
    应用内文件getFilesDir()...531.0Byte

    // 调用该方法,清除应用缓存

    DataCleanManager.cleanApplicationData(
    mActivity, DataCleanManager.getCacheSize(mActivity.getCacheDir()));

  • 相关阅读:
    1265 四点共面
    1003 阶乘后面0的数量
    1080 两个数的平方和
    1090 3个数和为0
    1087 1 10 100 1000
    1082 与7无关的数
    OpenLayers工作原理
    CI(持续集成)CD(持续交付)
    打包命令
    文件与目录管理重点
  • 原文地址:https://www.cnblogs.com/androidsj/p/4867276.html
Copyright © 2011-2022 走看看