15
15
*******************************************************************************/
16
16
package com .nostra13 .universalimageloader .utils ;
17
17
18
+ import static android .os .Environment .MEDIA_MOUNTED ;
19
+
18
20
import java .io .File ;
19
21
import java .io .IOException ;
20
22
30
32
*/
31
33
public final class StorageUtils {
32
34
35
+ private static final String EXTERNAL_STORAGE_PERMISSION = "android.permission.WRITE_EXTERNAL_STORAGE" ;
33
36
private static final String INDIVIDUAL_DIR_NAME = "uil-images" ;
34
37
35
38
private StorageUtils () {
36
39
}
37
40
38
41
/**
39
42
* Returns application cache directory. Cache directory will be created on SD card
40
- * <i>("/Android/data/[app_package_name]/cache")</i> if card is mounted. Else - Android defines cache directory on
41
- * device's file system.
43
+ * <i>("/Android/data/[app_package_name]/cache")</i> if card is mounted and app has appropriate permission. Else -
44
+ * Android defines cache directory on device's file system.
42
45
*
43
46
* @param context Application context
44
47
* @return Cache {@link File directory}
45
48
*/
46
49
public static File getCacheDirectory (Context context ) {
47
50
File appCacheDir = null ;
48
- if (Environment .getExternalStorageState ().equals (android . os . Environment . MEDIA_MOUNTED ) && hasExternalPermission (context )) {
51
+ if (Environment .getExternalStorageState ().equals (MEDIA_MOUNTED ) && hasExternalStoragePermission (context )) {
49
52
appCacheDir = getExternalCacheDir (context );
50
53
}
51
54
if (appCacheDir == null ) {
@@ -60,8 +63,8 @@ public static File getCacheDirectory(Context context) {
60
63
61
64
/**
62
65
* Returns individual application cache directory (for only image caching from ImageLoader). Cache directory will be
63
- * created on SD card <i>("/Android/data/[app_package_name]/cache/uil-images")</i> if card is mounted. Else -
64
- * Android defines cache directory on device's file system.
66
+ * created on SD card <i>("/Android/data/[app_package_name]/cache/uil-images")</i> if card is mounted and app has
67
+ * appropriate permission. Else - Android defines cache directory on device's file system.
65
68
*
66
69
* @param context Application context
67
70
* @return Cache {@link File directory}
@@ -79,15 +82,15 @@ public static File getIndividualCacheDirectory(Context context) {
79
82
80
83
/**
81
84
* Returns specified application cache directory. Cache directory will be created on SD card by defined path if card
82
- * is mounted. Else - Android defines cache directory on device's file system.
85
+ * is mounted and app has appropriate permission . Else - Android defines cache directory on device's file system.
83
86
*
84
87
* @param context Application context
85
88
* @param cacheDir Cache directory path (e.g.: "AppCacheDir", "AppDir/cache/images")
86
89
* @return Cache {@link File directory}
87
90
*/
88
91
public static File getOwnCacheDirectory (Context context , String cacheDir ) {
89
92
File appCacheDir = null ;
90
- if (Environment .getExternalStorageState ().equals (android . os . Environment . MEDIA_MOUNTED ) && hasExternalPermission (context )) {
93
+ if (Environment .getExternalStorageState ().equals (MEDIA_MOUNTED ) && hasExternalStoragePermission (context )) {
91
94
appCacheDir = new File (Environment .getExternalStorageDirectory (), cacheDir );
92
95
}
93
96
if (appCacheDir == null || (!appCacheDir .exists () && !appCacheDir .mkdirs ())) {
@@ -112,11 +115,9 @@ private static File getExternalCacheDir(Context context) {
112
115
}
113
116
return appCacheDir ;
114
117
}
115
-
116
- private static boolean hasExternalPermission (Context cxt )
117
- {
118
- String permission = "android.permission.WRITE_EXTERNAL_STORAGE" ;
119
- int res = cxt .checkCallingOrSelfPermission (permission );
120
- return (res == PackageManager .PERMISSION_GRANTED );
118
+
119
+ private static boolean hasExternalStoragePermission (Context context ) {
120
+ int perm = context .checkCallingOrSelfPermission (EXTERNAL_STORAGE_PERMISSION );
121
+ return perm == PackageManager .PERMISSION_GRANTED ;
121
122
}
122
123
}
0 commit comments