Skip to content

Commit 22fd32a

Browse files
author
Artur Chrusciel
committed
Catch exceptions on sd card directories constants creation. Methods to get sd card directories. sd card directiories as constants deprecated warning
1 parent 821eeb0 commit 22fd32a

File tree

4 files changed

+65
-10
lines changed

4 files changed

+65
-10
lines changed

android.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,25 @@ function addCompleteDownload(config) {
3838
return Promise.reject('RNFetchBlob.android.addCompleteDownload only supports Android.')
3939
}
4040

41+
function getSDCardDir() {
42+
if(Platform.OS === 'android')
43+
return RNFetchBlob.getSDCardDir()
44+
else
45+
return Promise.reject('RNFetchBlob.android.getSDCardDir only supports Android.')
46+
}
47+
48+
function getSDCardApplicationDir() {
49+
if(Platform.OS === 'android')
50+
return RNFetchBlob.getSDCardApplicationDir()
51+
else
52+
return Promise.reject('RNFetchBlob.android.getSDCardApplicationDir only supports Android.')
53+
}
54+
4155

4256
export default {
4357
actionViewIntent,
4458
getContentIntent,
45-
addCompleteDownload
59+
addCompleteDownload,
60+
getSDCardDir,
61+
getSDCardApplicationDir,
4662
}

android/src/main/java/com/RNFetchBlob/RNFetchBlob.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ public void run() {
9292
RNFetchBlobFS.createFile(path, content, encode, callback);
9393
}
9494
});
95-
9695
}
9796

9897
@ReactMethod
@@ -136,7 +135,6 @@ public void run() {
136135
RNFetchBlobFS.createFileASCII(path, dataArray, callback);
137136
}
138137
});
139-
140138
}
141139

142140
@ReactMethod
@@ -167,7 +165,6 @@ public void run() {
167165
RNFetchBlobFS.cp(path, dest, callback);
168166
}
169167
});
170-
171168
}
172169

173170
@ReactMethod
@@ -228,7 +225,6 @@ public void run() {
228225
RNFetchBlobFS.writeFile(path, encoding, data, append, promise);
229226
}
230227
});
231-
232228
}
233229

234230
@ReactMethod
@@ -263,7 +259,6 @@ public void run() {
263259
new RNFetchBlobFS(ctx).scanFile(p, m, callback);
264260
}
265261
});
266-
267262
}
268263

269264
@ReactMethod
@@ -324,7 +319,7 @@ public void enableUploadProgressReport(String taskId, int interval, int count) {
324319
@ReactMethod
325320
public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
326321
new RNFetchBlobReq(options, taskId, method, url, headers, body, null, mClient, callback).run();
327-
}
322+
}
328323

329324
@ReactMethod
330325
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) {
370365

371366
}
372367

368+
@ReactMethod
369+
public void getSDCardDir(Promise promise) {
370+
RNFetchBlobFS.getSDCardDir(promise);
371+
}
372+
373+
@ReactMethod
374+
public void getSDCardApplicationDir(Promise promise) {
375+
RNFetchBlobFS.getSDCardApplicationDir(this.getReactApplicationContext(), promise);
376+
}
373377
}

android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,38 @@ static public Map<String, Object> getSystemfolders(ReactApplicationContext ctx)
213213
state = Environment.getExternalStorageState();
214214
if (state.equals(Environment.MEDIA_MOUNTED)) {
215215
res.put("SDCardDir", Environment.getExternalStorageDirectory().getAbsolutePath());
216-
res.put("SDCardApplicationDir", ctx.getExternalFilesDir(null).getParentFile().getAbsolutePath());
216+
try {
217+
res.put("SDCardApplicationDir", ctx.getExternalFilesDir(null).getParentFile().getAbsolutePath());
218+
} catch(Exception e) {
219+
res.put("SDCardApplicationDir", "");
220+
}
217221
}
218222
res.put("MainBundleDir", ctx.getApplicationInfo().dataDir);
219223
return res;
220224
}
221225

226+
static public void getSDCardDir(Promise promise) {
227+
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
228+
promise.resolve(Environment.getExternalStorageDirectory().getAbsolutePath());
229+
} else {
230+
promise.reject("RNFetchBlob.getSDCardDir", "External storage not mounted");
231+
}
232+
233+
}
234+
235+
static public void getSDCardApplicationDir(ReactApplicationContext ctx, Promise promise) {
236+
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
237+
try {
238+
final String path = ctx.getExternalFilesDir(null).getParentFile().getAbsolutePath();
239+
promise.resolve(path);
240+
} catch (Exception e) {
241+
promise.reject("RNFetchBlob.getSDCardApplicationDir", e.getLocalizedMessage());
242+
}
243+
} else {
244+
promise.reject("RNFetchBlob.getSDCardApplicationDir", "External storage not mounted");
245+
}
246+
}
247+
222248
/**
223249
* Static method that returns a temp file path
224250
* @param ctx React Native application context

fs.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,17 @@ const dirs = {
2828
MovieDir : RNFetchBlob.MovieDir,
2929
DownloadDir : RNFetchBlob.DownloadDir,
3030
DCIMDir : RNFetchBlob.DCIMDir,
31-
SDCardDir : RNFetchBlob.SDCardDir,
32-
SDCardApplicationDir : RNFetchBlob.SDCardApplicationDir,
31+
get SDCardDir() {
32+
console.warn('SDCardDir as a constant is deprecated and will be removed in feature release. ' +
33+
'Use RNFetchBlob.android.getSDCardDir():Promise instead.');
34+
return RNFetchBlob.SDCardDir;
35+
},
36+
get SDCardApplicationDir() {
37+
console.warn('SDCardApplicationDir as a constant is deprecated and will be removed in feature release. ' +
38+
'Use RNFetchBlob.android.getSDCardApplicationDir():Promise instead. ' +
39+
'This variable can be empty on error in native code.');
40+
return RNFetchBlob.SDCardApplicationDir;
41+
},
3342
MainBundleDir : RNFetchBlob.MainBundleDir,
3443
LibraryDir : RNFetchBlob.LibraryDir
3544
}

0 commit comments

Comments
 (0)