@@ -252,20 +252,20 @@ static Map<String, Object> getSystemfolders(ReactApplicationContext ctx) {
252
252
253
253
res .put ("DocumentDir" , ctx .getFilesDir ().getAbsolutePath ());
254
254
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 ();
263
263
if (state .equals (Environment .MEDIA_MOUNTED )) {
264
- res .put ("SDCardDir" , ctx . getExternalFilesDir ( null ). getAbsolutePath ( ));
264
+ res .put ("SDCardDir" , getExternalFilesDirPath ( ctx , null ));
265
265
266
266
File externalDirectory = ctx .getExternalFilesDir (null );
267
267
268
- if (externalDirectory != null ) {
268
+ if (externalDirectory != null && externalDirectory . getParentFile () != null ) {
269
269
res .put ("SDCardApplicationDir" , externalDirectory .getParentFile ().getAbsolutePath ());
270
270
} else {
271
271
res .put ("SDCardApplicationDir" , "" );
@@ -276,9 +276,20 @@ static Map<String, Object> getSystemfolders(ReactApplicationContext ctx) {
276
276
return res ;
277
277
}
278
278
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
+
279
285
static public void getSDCardDir (ReactApplicationContext ctx , Promise promise ) {
280
286
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
+ }
282
293
} else {
283
294
promise .reject ("ReactNativeBlobUtil.getSDCardDir" , "External storage not mounted" );
284
295
}
@@ -992,12 +1003,17 @@ static void df(Callback callback, ReactApplicationContext ctx) {
992
1003
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN_MR2 ) {
993
1004
args .putString ("internal_free" , String .valueOf (stat .getFreeBytes ()));
994
1005
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
+ }
999
1015
}
1000
- callback .invoke (null , args );
1016
+ callback .invoke (null , args );
1001
1017
}
1002
1018
1003
1019
/**
0 commit comments