Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit b84c9fd

Browse files
committed
Add customized path support for Android Download Manager #74
1 parent 3373932 commit b84c9fd

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,15 @@ public void run() {
127127
Uri uri = Uri.parse(url);
128128
DownloadManager.Request req = new DownloadManager.Request(uri);
129129
req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
130-
131-
if (options.addAndroidDownloads.hasKey("title")) {
130+
if(options.addAndroidDownloads.hasKey("title")) {
132131
req.setTitle(options.addAndroidDownloads.getString("title"));
133132
}
134-
if (options.addAndroidDownloads.hasKey("description")) {
133+
if(options.addAndroidDownloads.hasKey("description")) {
135134
req.setDescription(options.addAndroidDownloads.getString("description"));
136135
}
136+
if(options.addAndroidDownloads.hasKey("path")) {
137+
req.setDestinationUri(Uri.parse("file://" + options.addAndroidDownloads.getString("path")));
138+
}
137139
// set headers
138140
ReadableMapKeySetIterator it = headers.keySetIterator();
139141
while (it.hasNextKey()) {
@@ -553,14 +555,28 @@ public void onReceive(Context context, Intent intent) {
553555
String contentUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
554556
Uri uri = Uri.parse(contentUri);
555557
Cursor cursor = appCtx.getContentResolver().query(uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
558+
// use default destination of DownloadManager
556559
if (cursor != null) {
557560
cursor.moveToFirst();
558561
String filePath = cursor.getString(0);
559562
cursor.close();
560-
this.callback.invoke(null, null, filePath);
563+
this.callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, filePath);
564+
}
565+
// custom destination
566+
else {
567+
if(options.addAndroidDownloads.hasKey("path")) {
568+
try {
569+
String customDest = options.addAndroidDownloads.getString("path");
570+
boolean exists = new File(customDest).exists();
571+
if(!exists)
572+
throw new Exception("Download manager download failed, the file does not downloaded to destination.");
573+
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, customDest);
574+
575+
} catch(Exception ex) {
576+
this.callback.invoke(ex.getLocalizedMessage(), null, null);
577+
}
578+
}
561579
}
562-
else
563-
this.callback.invoke(null, null, null);
564580
}
565581
}
566582
}

0 commit comments

Comments
 (0)