StorageUtils.java
3.12 KB
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
package com.danikula.videocache;
import android.content.Context;
import android.os.Environment;
import java.io.File;
import static android.os.Environment.MEDIA_MOUNTED;
/**
* Provides application storage paths
* <p/>
* See https://github.com/nostra13/Android-Universal-Image-Loader
*
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
* @since 1.0.0
*/
public final class StorageUtils {
private static final String INDIVIDUAL_DIR_NAME = "video-cache";
/**
* Returns individual application cache directory (for only video caching from Proxy). Cache directory will be
* created on SD card <i>("/Android/data/[app_package_name]/cache/video-cache")</i> if card is mounted .
* Else - Android defines cache directory on device's file system.
*
* @param context Application context
* @return Cache {@link File directory}
*/
static File getIndividualCacheDirectory(Context context) {
File cacheDir = getCacheDirectory(context);
return new File(cacheDir, INDIVIDUAL_DIR_NAME);
}
/**
* Returns application cache directory. Cache directory will be created on SD card
* <i>("/Android/data/[app_package_name]/cache")</i> (if card is mounted and app has appropriate permission) or
* on device's file system depending incoming parameters.
*
* @param context Application context
* @return Cache {@link File directory}.<br />
* <b>NOTE:</b> Can be null in some unpredictable cases (if SD card is unmounted and
* {@link Context#getCacheDir() Context.getCacheDir()} returns null).
*/
private static File getCacheDirectory(Context context) {
File appCacheDir = null;
if (MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
appCacheDir = context.getExternalCacheDir();
}
if (appCacheDir == null) {
appCacheDir = context.getCacheDir();
}
if (appCacheDir == null) {
String cacheDirPath = "/data/data/" + context.getPackageName() + "/cache/";
appCacheDir = new File(cacheDirPath);
}
return appCacheDir;
}
/**
* delete directory
*/
public static boolean deleteFiles(File root) {
File[] files = root.listFiles();
if (files != null) {
for (File f : files) {
if (!f.isDirectory() && f.exists()) { // 判断是否存在
if (!f.delete()) {
return false;
}
}
}
}
return true;
}
/**
* delete file
*/
public static boolean deleteFile(String filePath) {
File file = new File(filePath);
if (file.exists()) {
if (file.isFile()) {
return file.delete();
} else {
String[] filePaths = file.list();
if (filePaths != null) {
for (String path : filePaths) {
deleteFile(filePath + File.separator + path);
}
}
return file.delete();
}
}
return true;
}
}