Skip to content

Commit 9f6dbcc

Browse files
committed
Merge pull request nostra13#311 from sherifelkhatib/master
Allowing Apps to omit "android.permission.WRITE_EXTERNAL_STORAGE" even when sdcard is mounted
2 parents 022f4e2 + 31e62dc commit 9f6dbcc

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

library/src/com/nostra13/universalimageloader/utils/StorageUtils.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020

2121
import android.content.Context;
22+
import android.content.pm.PackageManager;
2223
import android.os.Environment;
2324

2425
/**
@@ -44,7 +45,7 @@ private StorageUtils() {
4445
*/
4546
public static File getCacheDirectory(Context context) {
4647
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)) {
4849
appCacheDir = getExternalCacheDir(context);
4950
}
5051
if (appCacheDir == null) {
@@ -86,7 +87,7 @@ public static File getIndividualCacheDirectory(Context context) {
8687
*/
8788
public static File getOwnCacheDirectory(Context context, String cacheDir) {
8889
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)) {
9091
appCacheDir = new File(Environment.getExternalStorageDirectory(), cacheDir);
9192
}
9293
if (appCacheDir == null || (!appCacheDir.exists() && !appCacheDir.mkdirs())) {
@@ -111,4 +112,11 @@ private static File getExternalCacheDir(Context context) {
111112
}
112113
return appCacheDir;
113114
}
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+
}
114122
}

0 commit comments

Comments
 (0)