Skip to content

Commit ae998b2

Browse files
committed
Add null checks to getExternalFilesDir calls
1 parent fb1d70d commit ae998b2

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

android/src/main/java/com/ReactNativeBloBUtil/Utils/PathResolver.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public static String getRealPathFromURI(final Context context, final Uri uri) {
2929
final String type = split[0];
3030

3131
if ("primary".equalsIgnoreCase(type)) {
32-
return context.getExternalFilesDir(null) + "/" + split[1];
32+
File dir = context.getExternalFilesDir(null);
33+
if (dir != null) return dir + "/" + split[1];
34+
return "";
3335
}
3436

3537
// TODO handle non-primary volumes

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFS.java

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -252,20 +252,20 @@ static Map<String, Object> getSystemfolders(ReactApplicationContext ctx) {
252252

253253
res.put("DocumentDir", ctx.getFilesDir().getAbsolutePath());
254254
res.put("CacheDir", ctx.getCacheDir().getAbsolutePath());
255-
res.put("DCIMDir", ctx.getExternalFilesDir(Environment.DIRECTORY_DCIM).getAbsolutePath());
256-
res.put("PictureDir", ctx.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath());
257-
res.put("MusicDir", ctx.getExternalFilesDir(Environment.DIRECTORY_MUSIC).getAbsolutePath());
258-
res.put("DownloadDir", ctx.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
259-
res.put("MovieDir", ctx.getExternalFilesDir(Environment.DIRECTORY_MOVIES).getAbsolutePath());
260-
res.put("RingtoneDir", ctx.getExternalFilesDir(Environment.DIRECTORY_RINGTONES).getAbsolutePath());
261-
String state;
262-
state = Environment.getExternalStorageState();
255+
res.put("DCIMDir", getExternalFilesDirPath(ctx, Environment.DIRECTORY_DCIM));
256+
res.put("PictureDir", getExternalFilesDirPath(ctx, Environment.DIRECTORY_PICTURES));
257+
res.put("MusicDir", getExternalFilesDirPath(ctx, Environment.DIRECTORY_MUSIC));
258+
res.put("DownloadDir", getExternalFilesDirPath(ctx, Environment.DIRECTORY_DOWNLOADS));
259+
res.put("MovieDir", getExternalFilesDirPath(ctx, Environment.DIRECTORY_MOVIES));
260+
res.put("RingtoneDir", getExternalFilesDirPath(ctx, Environment.DIRECTORY_RINGTONES));
261+
262+
String state = Environment.getExternalStorageState();
263263
if (state.equals(Environment.MEDIA_MOUNTED)) {
264-
res.put("SDCardDir", ctx.getExternalFilesDir(null).getAbsolutePath());
264+
res.put("SDCardDir", getExternalFilesDirPath(ctx, null));
265265

266266
File externalDirectory = ctx.getExternalFilesDir(null);
267267

268-
if (externalDirectory != null) {
268+
if (externalDirectory != null && externalDirectory.getParentFile() != null) {
269269
res.put("SDCardApplicationDir", externalDirectory.getParentFile().getAbsolutePath());
270270
} else {
271271
res.put("SDCardApplicationDir", "");
@@ -276,9 +276,20 @@ static Map<String, Object> getSystemfolders(ReactApplicationContext ctx) {
276276
return res;
277277
}
278278

279+
static String getExternalFilesDirPath(ReactApplicationContext ctx, String type) {
280+
File dir = ctx.getExternalFilesDir(type);
281+
if (dir != null) return dir.getAbsolutePath();
282+
return "";
283+
}
284+
279285
static public void getSDCardDir(ReactApplicationContext ctx, Promise promise) {
280286
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
281-
promise.resolve(ctx.getExternalFilesDir(null).getAbsolutePath());
287+
try {
288+
final String path = ctx.getExternalFilesDir(null).getAbsolutePath();
289+
promise.resolve(path);
290+
} catch (Exception e) {
291+
promise.reject("ReactNativeBlobUtil.getSDCardDir", e.getLocalizedMessage());
292+
}
282293
} else {
283294
promise.reject("ReactNativeBlobUtil.getSDCardDir", "External storage not mounted");
284295
}
@@ -992,12 +1003,17 @@ static void df(Callback callback, ReactApplicationContext ctx) {
9921003
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
9931004
args.putString("internal_free", String.valueOf(stat.getFreeBytes()));
9941005
args.putString("internal_total", String.valueOf(stat.getTotalBytes()));
995-
StatFs statEx = new StatFs(ctx.getExternalFilesDir(null).getPath());
996-
args.putString("external_free", String.valueOf(statEx.getFreeBytes()));
997-
args.putString("external_total", String.valueOf(statEx.getTotalBytes()));
998-
1006+
File dir = ctx.getExternalFilesDir(null);
1007+
if (dir != null) {
1008+
StatFs statEx = new StatFs(dir.getPath());
1009+
args.putString("external_free", String.valueOf(statEx.getFreeBytes()));
1010+
args.putString("external_total", String.valueOf(statEx.getTotalBytes()));
1011+
} else {
1012+
args.putString("external_free", "-1");
1013+
args.putString("external_total", "-1");
1014+
}
9991015
}
1000-
callback.invoke(null ,args);
1016+
callback.invoke(null, args);
10011017
}
10021018

10031019
/**

0 commit comments

Comments
 (0)