Browse Source

Add customized path support for Android Download Manager #74

Ben Hsieh 8 years ago
parent
commit
b84c9fde1d
1 changed files with 22 additions and 6 deletions
  1. 22
    6
      src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

+ 22
- 6
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

127
                 Uri uri = Uri.parse(url);
127
                 Uri uri = Uri.parse(url);
128
                 DownloadManager.Request req = new DownloadManager.Request(uri);
128
                 DownloadManager.Request req = new DownloadManager.Request(uri);
129
                 req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
129
                 req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
130
-
131
-                if (options.addAndroidDownloads.hasKey("title")) {
130
+                if(options.addAndroidDownloads.hasKey("title")) {
132
                     req.setTitle(options.addAndroidDownloads.getString("title"));
131
                     req.setTitle(options.addAndroidDownloads.getString("title"));
133
                 }
132
                 }
134
-                if (options.addAndroidDownloads.hasKey("description")) {
133
+                if(options.addAndroidDownloads.hasKey("description")) {
135
                     req.setDescription(options.addAndroidDownloads.getString("description"));
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
                 // set headers
139
                 // set headers
138
                 ReadableMapKeySetIterator it = headers.keySetIterator();
140
                 ReadableMapKeySetIterator it = headers.keySetIterator();
139
                 while (it.hasNextKey()) {
141
                 while (it.hasNextKey()) {
553
                     String contentUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
555
                     String contentUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
554
                     Uri uri = Uri.parse(contentUri);
556
                     Uri uri = Uri.parse(contentUri);
555
                     Cursor cursor = appCtx.getContentResolver().query(uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
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
                     if (cursor != null) {
559
                     if (cursor != null) {
557
                         cursor.moveToFirst();
560
                         cursor.moveToFirst();
558
                         String filePath = cursor.getString(0);
561
                         String filePath = cursor.getString(0);
559
                         cursor.close();
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
         }