19
19
import java .io .IOException ;
20
20
21
21
import android .content .Context ;
22
+ import android .content .pm .PackageManager ;
22
23
import android .os .Environment ;
23
24
24
25
/**
@@ -44,7 +45,7 @@ private StorageUtils() {
44
45
*/
45
46
public static File getCacheDirectory (Context context ) {
46
47
File appCacheDir = null ;
47
- if (Environment .getExternalStorageState ().equals (android .os .Environment .MEDIA_MOUNTED )) {
48
+ if (Environment .getExternalStorageState ().equals (android .os .Environment .MEDIA_MOUNTED ) && hasExternalPermission ( context ) ) {
48
49
appCacheDir = getExternalCacheDir (context );
49
50
}
50
51
if (appCacheDir == null ) {
@@ -86,7 +87,7 @@ public static File getIndividualCacheDirectory(Context context) {
86
87
*/
87
88
public static File getOwnCacheDirectory (Context context , String cacheDir ) {
88
89
File appCacheDir = null ;
89
- if (Environment .getExternalStorageState ().equals (android .os .Environment .MEDIA_MOUNTED )) {
90
+ if (Environment .getExternalStorageState ().equals (android .os .Environment .MEDIA_MOUNTED ) && hasExternalPermission ( context ) ) {
90
91
appCacheDir = new File (Environment .getExternalStorageDirectory (), cacheDir );
91
92
}
92
93
if (appCacheDir == null || (!appCacheDir .exists () && !appCacheDir .mkdirs ())) {
@@ -111,4 +112,11 @@ private static File getExternalCacheDir(Context context) {
111
112
}
112
113
return appCacheDir ;
113
114
}
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 );
121
+ }
114
122
}
0 commit comments