Welcome to Ray's Blog

Stay Hungry Stay Foolish - Steve Jobs

0%

Android清除缓存工具类


概述

项目需要实现清除缓存的功能,网上找了一圈之后,参考了> csdn-IT_xiao 小巫 <的代码,根据项目实际,做了部分代码修改/优化。主要用以备忘,如果侵权,告知即删。

基础知识

手机文件内部分类:

  • 系统文件
  • 内置存储卡 ROM
  • 外置存储卡 SD 卡

应用存放文件路径:(备注:中间 xxx 为应用包名)

  • /data/data/com.xxx.xxx/cache - 应用内缓存(注:对应方法 getCacheDir())
  • /data/data/com.xxx.xxx/databases - 应用内数据库
  • /data/data/com.xxx.xxx/shared_prefs - 应用内配置文件
  • /data/data/com.xxx.xxx/files - 应用内文件(注:对应方法 getFilesDir())

判断 SD 卡是否存在

1
2
3
4
5
6
7
private boolean ExistSDCard() {
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
return true;
} else
return false;
}

代码工具类

功能列表:

  • 获取内外内存 cache 文件夹数据大小;
  • 格式化内存大小为带单位字符串;
  • 清除缓存;
  • 清除 app 所有数据;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227

/**
* Date: 20170811
* Description: 文件数据管理类
*/
public class FileDataManager {

private static final String TAG = FileDataManager.class.getName();

/**
* 获取缓存文件夹大小
* 主要获取内置/外置内存卡 cache文件夹下面所有的文件和文件夹
*/
public static long getCacheDirSize(Context context) {
try {
return getFolderSize(context.getExternalCacheDir()) + getFolderSize(context.getCacheDir());
} catch (Exception e) {
return 0L;
}
}

/**
* 清除本应用所有的数据
*/
public static void cleanAllAppData(Context context, String... filepath) {
cleanInternalCache(context);
cleanExternalCache(context);
cleanDatabases(context);
cleanSharedPreference(context);
cleanFiles(context);
if (filepath == null || filepath.length <= 0) return;
for (String filePath : filepath) {
cleanCustomCache(filePath);
}
}

/**
* 清除本应用所有缓存数据
*/
public static void cleanAppCacheData(Context context, String... filepath) {
cleanInternalCache(context);
cleanExternalCache(context);
if (filepath != null && filepath.length > 0) {
for (String filePath : filepath) {
cleanCustomCache(filePath);
}
}
EventBus.getDefault().post(AppConstants.EVENT_CLEAN_CACHE);
}

/**
* 清除本应用内部缓存(/data/data/com.xxx.xxx/cache)
*/
public static void cleanInternalCache(Context context) {
deleteFolderFile(context.getCacheDir(), false);
}

/**
* 清除本应用所有数据库(/data/data/com.xxx.xxx/databases) * * @param context
*/
public static void cleanDatabases(Context context) {
deleteFolderFile(new File("/data/data/"
+ context.getPackageName() + "/databases"),false);

}

/**
* * 清除本应用SharedPreference(/data/data/com.xxx.xxx/shared_prefs) * * @param
* context
*/
public static void cleanSharedPreference(Context context) {
deleteFolderFile(new File("/data/data/"
+ context.getPackageName() + "/shared_prefs"),false);
}

/**
* 按名字清除本应用数据库
*
* @param dbName 数据库名称
*/
public static void cleanDatabaseByName(Context context, String dbName) {
context.deleteDatabase(dbName);
}

/**
* 清除/data/data/com.xxx.xxx/files下的内容
*/
public static void cleanFiles(Context context) {
deleteFolderFile(context.getFilesDir(), false);
}

/**
* 清除外部cache下的内容(/mnt/sdcard/android/data/com.xxx.xxx/cache)
*/
public static void cleanExternalCache(Context context) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
deleteFolderFile(context.getExternalCacheDir(), false);
}
}

/**
* 清除自定义路径下的文件,使用需小心,请不要误删。而且只支持目录下的文件删除
*
* @param filePath 文件路径
*/
public static void cleanCustomCache(String filePath) {
deleteFolderFile(filePath, false);
}

/**
* 获取缓存文件大小
* @param filePath 文件路径
*/
public static long getFolderSize(String filePath) {
if (StringUtils.isEmpty(filePath)) {
return 0L;
}
return getFolderSize(new File(filePath));
}

/**
* 获取缓存文件大小
* Context.getExternalFilesDir() --> SDCard/Android/data/你的应用的包名/files/ 目录,一般放一些长时间保存的数据
* Context.getExternalCacheDir() --> SDCard/Android/data/你的应用包名/cache/目录,一般存放临时缓存数据
*
* @param file 具体文件
*/
public static long getFolderSize(File file) {
long size = 0;
try {
File[] fileList = file.listFiles();
for (int i = 0; i < fileList.length; i++) {
// 如果下面还有文件
if (fileList[i].isDirectory()) {
size = size + getFolderSize(fileList[i]);
} else {
size = size + fileList[i].length();
}
}
} catch (Exception e) {
e.printStackTrace();
}
LogUtils.d(TAG, "getFolderSize() called with: file = [" + file + "]--size = [" + size + "]");
return size;
}

/**
* 删除指定目录下文件及目录
*
* @param deleteThisPath 是否删除传入的文件
* @param file 文件
*/
public static void deleteFolderFile(File file, boolean deleteThisPath) {
if (file!=null) {
try {
if (file.isDirectory()) {// 如果下面还有文件
File files[] = file.listFiles();
for (int i = 0; i < files.length; i++) {
deleteFolderFile(files[i].getAbsolutePath(), true);
}
}
if (deleteThisPath) {
if (!file.isDirectory()) {// 如果是文件,删除
file.delete();
} else {// 目录
if (file.listFiles().length == 0) {// 目录下没有文件或者目录,删除
file.delete();
}
}
}
LogUtils.d(TAG, "deleteFolderFile() called with: file = [" + file + "], deleteThisPath = [" + deleteThisPath + "]");
} catch (Exception e) {
e.printStackTrace();
}
}
}


/**
* 删除指定目录下文件及目录
*
* @param deleteThisPath 是否删除传入的文件
* @param filePath 文件路径
*/
public static void deleteFolderFile(String filePath, boolean deleteThisPath) {
if (!TextUtils.isEmpty(filePath)) {
deleteFolderFile(new File(filePath),deleteThisPath);
}
}

/**
* 格式化文件大小带不同的单位
*
* @param size 文件大小
*/
public static String getFormatSize(double size) {
double kiloByte = size / 1024;
if (kiloByte < 1) {
return size + " Byte";
}

double megaByte = kiloByte / 1024;
if (megaByte < 1) {
BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
return result1.setScale(2, BigDecimal.ROUND_HALF_UP)
.toPlainString() + "KB";
}

double gigaByte = megaByte / 1024;
if (gigaByte < 1) {
BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
return result2.setScale(2, BigDecimal.ROUND_HALF_UP)
.toPlainString() + "MB";
}

double teraBytes = gigaByte / 1024;
if (teraBytes < 1) {
BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
return result3.setScale(2, BigDecimal.ROUND_HALF_UP)
.toPlainString() + "GB";
}
BigDecimal result4 = new BigDecimal(teraBytes);
return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()
+ "TB";
}
}

总结

Enjoys!