diff --git a/android.js b/android.js index 0b5dbd84b..c9abd283a 100644 --- a/android.js +++ b/android.js @@ -38,9 +38,25 @@ function addCompleteDownload(config) { return Promise.reject('RNFetchBlob.android.addCompleteDownload only supports Android.') } +function getSDCardDir() { + if(Platform.OS === 'android') + return RNFetchBlob.getSDCardDir() + else + return Promise.reject('RNFetchBlob.android.getSDCardDir only supports Android.') +} + +function getSDCardApplicationDir() { + if(Platform.OS === 'android') + return RNFetchBlob.getSDCardApplicationDir() + else + return Promise.reject('RNFetchBlob.android.getSDCardApplicationDir only supports Android.') +} + export default { actionViewIntent, getContentIntent, - addCompleteDownload + addCompleteDownload, + getSDCardDir, + getSDCardApplicationDir, } diff --git a/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java b/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java index 19e1be435..f5597d3ad 100644 --- a/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java +++ b/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java @@ -92,7 +92,6 @@ public void run() { RNFetchBlobFS.createFile(path, content, encode, callback); } }); - } @ReactMethod @@ -136,7 +135,6 @@ public void run() { RNFetchBlobFS.createFileASCII(path, dataArray, callback); } }); - } @ReactMethod @@ -167,7 +165,6 @@ public void run() { RNFetchBlobFS.cp(path, dest, callback); } }); - } @ReactMethod @@ -228,7 +225,6 @@ public void run() { RNFetchBlobFS.writeFile(path, encoding, data, append, promise); } }); - } @ReactMethod @@ -263,7 +259,6 @@ public void run() { new RNFetchBlobFS(ctx).scanFile(p, m, callback); } }); - } @ReactMethod @@ -324,7 +319,7 @@ public void enableUploadProgressReport(String taskId, int interval, int count) { @ReactMethod public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) { new RNFetchBlobReq(options, taskId, method, url, headers, body, null, mClient, callback).run(); -} + } @ReactMethod public void fetchBlobForm(ReadableMap options, String taskId, String method, String url, ReadableMap headers, ReadableArray body, final Callback callback) { @@ -370,4 +365,13 @@ public void addCompleteDownload (ReadableMap config, Promise promise) { } + @ReactMethod + public void getSDCardDir(Promise promise) { + RNFetchBlobFS.getSDCardDir(promise); + } + + @ReactMethod + public void getSDCardApplicationDir(Promise promise) { + RNFetchBlobFS.getSDCardApplicationDir(this.getReactApplicationContext(), promise); + } } diff --git a/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java b/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java index 7a7910546..0959c4f9f 100644 --- a/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java +++ b/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java @@ -213,12 +213,38 @@ static public Map getSystemfolders(ReactApplicationContext ctx) state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { res.put("SDCardDir", Environment.getExternalStorageDirectory().getAbsolutePath()); - res.put("SDCardApplicationDir", ctx.getExternalFilesDir(null).getParentFile().getAbsolutePath()); + try { + res.put("SDCardApplicationDir", ctx.getExternalFilesDir(null).getParentFile().getAbsolutePath()); + } catch(Exception e) { + res.put("SDCardApplicationDir", ""); + } } res.put("MainBundleDir", ctx.getApplicationInfo().dataDir); return res; } + static public void getSDCardDir(Promise promise) { + if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { + promise.resolve(Environment.getExternalStorageDirectory().getAbsolutePath()); + } else { + promise.reject("RNFetchBlob.getSDCardDir", "External storage not mounted"); + } + + } + + static public void getSDCardApplicationDir(ReactApplicationContext ctx, Promise promise) { + if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { + try { + final String path = ctx.getExternalFilesDir(null).getParentFile().getAbsolutePath(); + promise.resolve(path); + } catch (Exception e) { + promise.reject("RNFetchBlob.getSDCardApplicationDir", e.getLocalizedMessage()); + } + } else { + promise.reject("RNFetchBlob.getSDCardApplicationDir", "External storage not mounted"); + } + } + /** * Static method that returns a temp file path * @param ctx React Native application context diff --git a/fs.js b/fs.js index 83e6bdceb..f50691e5e 100644 --- a/fs.js +++ b/fs.js @@ -28,8 +28,17 @@ const dirs = { MovieDir : RNFetchBlob.MovieDir, DownloadDir : RNFetchBlob.DownloadDir, DCIMDir : RNFetchBlob.DCIMDir, - SDCardDir : RNFetchBlob.SDCardDir, - SDCardApplicationDir : RNFetchBlob.SDCardApplicationDir, + get SDCardDir() { + console.warn('SDCardDir as a constant is deprecated and will be removed in feature release. ' + + 'Use RNFetchBlob.android.getSDCardDir():Promise instead.'); + return RNFetchBlob.SDCardDir; + }, + get SDCardApplicationDir() { + console.warn('SDCardApplicationDir as a constant is deprecated and will be removed in feature release. ' + + 'Use RNFetchBlob.android.getSDCardApplicationDir():Promise instead. ' + + 'This variable can be empty on error in native code.'); + return RNFetchBlob.SDCardApplicationDir; + }, MainBundleDir : RNFetchBlob.MainBundleDir, LibraryDir : RNFetchBlob.LibraryDir }