Quellcode durchsuchen

Add customized path support for Android Download Manager #74

Ben Hsieh vor 8 Jahren
Ursprung
Commit
b84c9fde1d
1 geänderte Dateien mit 22 neuen und 6 gelöschten Zeilen
  1. 22
    6
      src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

+ 22
- 6
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java Datei anzeigen

@@ -127,13 +127,15 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
127 127
                 Uri uri = Uri.parse(url);
128 128
                 DownloadManager.Request req = new DownloadManager.Request(uri);
129 129
                 req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
130
-
131
-                if (options.addAndroidDownloads.hasKey("title")) {
130
+                if(options.addAndroidDownloads.hasKey("title")) {
132 131
                     req.setTitle(options.addAndroidDownloads.getString("title"));
133 132
                 }
134
-                if (options.addAndroidDownloads.hasKey("description")) {
133
+                if(options.addAndroidDownloads.hasKey("description")) {
135 134
                     req.setDescription(options.addAndroidDownloads.getString("description"));
136 135
                 }
136
+                if(options.addAndroidDownloads.hasKey("path")) {
137
+                    req.setDestinationUri(Uri.parse("file://" + options.addAndroidDownloads.getString("path")));
138
+                }
137 139
                 // set headers
138 140
                 ReadableMapKeySetIterator it = headers.keySetIterator();
139 141
                 while (it.hasNextKey()) {
@@ -553,14 +555,28 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
553 555
                     String contentUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
554 556
                     Uri uri = Uri.parse(contentUri);
555 557
                     Cursor cursor = appCtx.getContentResolver().query(uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
558
+                    // use default destination of DownloadManager
556 559
                     if (cursor != null) {
557 560
                         cursor.moveToFirst();
558 561
                         String filePath = cursor.getString(0);
559 562
                         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
+                        }
561 579
                     }
562
-                    else
563
-                        this.callback.invoke(null, null, null);
564 580
                 }
565 581
             }
566 582
         }