Browse Source

Add Android Download Manager status code handling and improve error message #297

Ben Hsieh 8 years ago
parent
commit
57a28e135d

+ 2
- 1
android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java View File

@@ -234,7 +234,8 @@ public class RNFetchBlobFS {
234 234
 
235 235
             InputStream fs;
236 236
             if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
237
-                fs = RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
237
+                fs = RNFetchBlob.RCTContext.getAssets()
238
+                        .open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
238 239
             }
239 240
             else {
240 241
                 fs = new FileInputStream(new File(path));

+ 10
- 2
android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

@@ -629,10 +629,17 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
629 629
                 DownloadManager dm = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);
630 630
                 dm.query(query);
631 631
                 Cursor c = dm.query(query);
632
-                String error = null;
632
+
633
+
633 634
                 String filePath = null;
634 635
                 // the file exists in media content database
635 636
                 if (c.moveToFirst()) {
637
+                    // #297 handle failed request
638
+                    int statusCode = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
639
+                    if(statusCode == DownloadManager.STATUS_FAILED) {
640
+                        this.callback.invoke("Download manager failed to download from  " + this.url + ". Statu Code = " + statusCode, null, null);
641
+                        return;
642
+                    }
636 643
                     String contentUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
637 644
                     Uri uri = Uri.parse(contentUri);
638 645
                     Cursor cursor = appCtx.getContentResolver().query(uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
@@ -653,7 +660,8 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
653 660
                             this.callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, customDest);
654 661
 
655 662
                     } catch(Exception ex) {
656
-                        error = ex.getLocalizedMessage();
663
+                        ex.printStackTrace();
664
+                        this.callback.invoke(ex.getLocalizedMessage(), null);
657 665
                     }
658 666
                 }
659 667
                 else {