Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add null checks to getExternalFilesDir calls #31

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public static String getRealPathFromURI(final Context context, final Uri uri) {
final String type = split[0];

if ("primary".equalsIgnoreCase(type)) {
return context.getExternalFilesDir(null) + "/" + split[1];
File dir = context.getExternalFilesDir(null);
if (dir != null) return dir + "/" + split[1];
return "";
}

// TODO handle non-primary volumes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,20 +252,20 @@ static Map<String, Object> getSystemfolders(ReactApplicationContext ctx) {

res.put("DocumentDir", ctx.getFilesDir().getAbsolutePath());
res.put("CacheDir", ctx.getCacheDir().getAbsolutePath());
res.put("DCIMDir", ctx.getExternalFilesDir(Environment.DIRECTORY_DCIM).getAbsolutePath());
res.put("PictureDir", ctx.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath());
res.put("MusicDir", ctx.getExternalFilesDir(Environment.DIRECTORY_MUSIC).getAbsolutePath());
res.put("DownloadDir", ctx.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
res.put("MovieDir", ctx.getExternalFilesDir(Environment.DIRECTORY_MOVIES).getAbsolutePath());
res.put("RingtoneDir", ctx.getExternalFilesDir(Environment.DIRECTORY_RINGTONES).getAbsolutePath());
String state;
state = Environment.getExternalStorageState();
res.put("DCIMDir", getExternalFilesDirPath(ctx, Environment.DIRECTORY_DCIM));
res.put("PictureDir", getExternalFilesDirPath(ctx, Environment.DIRECTORY_PICTURES));
res.put("MusicDir", getExternalFilesDirPath(ctx, Environment.DIRECTORY_MUSIC));
res.put("DownloadDir", getExternalFilesDirPath(ctx, Environment.DIRECTORY_DOWNLOADS));
res.put("MovieDir", getExternalFilesDirPath(ctx, Environment.DIRECTORY_MOVIES));
res.put("RingtoneDir", getExternalFilesDirPath(ctx, Environment.DIRECTORY_RINGTONES));

String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
res.put("SDCardDir", ctx.getExternalFilesDir(null).getAbsolutePath());
res.put("SDCardDir", getExternalFilesDirPath(ctx, null));

File externalDirectory = ctx.getExternalFilesDir(null);

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

static String getExternalFilesDirPath(ReactApplicationContext ctx, String type) {
File dir = ctx.getExternalFilesDir(type);
if (dir != null) return dir.getAbsolutePath();
return "";
}

static public void getSDCardDir(ReactApplicationContext ctx, Promise promise) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
promise.resolve(ctx.getExternalFilesDir(null).getAbsolutePath());
try {
final String path = ctx.getExternalFilesDir(null).getAbsolutePath();
promise.resolve(path);
} catch (Exception e) {
promise.reject("ReactNativeBlobUtil.getSDCardDir", e.getLocalizedMessage());
}
} else {
promise.reject("ReactNativeBlobUtil.getSDCardDir", "External storage not mounted");
}
Expand Down Expand Up @@ -992,12 +1003,17 @@ static void df(Callback callback, ReactApplicationContext ctx) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
args.putString("internal_free", String.valueOf(stat.getFreeBytes()));
args.putString("internal_total", String.valueOf(stat.getTotalBytes()));
StatFs statEx = new StatFs(ctx.getExternalFilesDir(null).getPath());
args.putString("external_free", String.valueOf(statEx.getFreeBytes()));
args.putString("external_total", String.valueOf(statEx.getTotalBytes()));

File dir = ctx.getExternalFilesDir(null);
if (dir != null) {
StatFs statEx = new StatFs(dir.getPath());
args.putString("external_free", String.valueOf(statEx.getFreeBytes()));
args.putString("external_total", String.valueOf(statEx.getTotalBytes()));
} else {
args.putString("external_free", "-1");
args.putString("external_total", "-1");
}
}
callback.invoke(null ,args);
callback.invoke(null, args);
}

/**
Expand Down