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
 
234
 
235
             InputStream fs;
235
             InputStream fs;
236
             if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
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
             else {
240
             else {
240
                 fs = new FileInputStream(new File(path));
241
                 fs = new FileInputStream(new File(path));

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

629
                 DownloadManager dm = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);
629
                 DownloadManager dm = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);
630
                 dm.query(query);
630
                 dm.query(query);
631
                 Cursor c = dm.query(query);
631
                 Cursor c = dm.query(query);
632
-                String error = null;
632
+
633
+
633
                 String filePath = null;
634
                 String filePath = null;
634
                 // the file exists in media content database
635
                 // the file exists in media content database
635
                 if (c.moveToFirst()) {
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
                     String contentUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
643
                     String contentUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
637
                     Uri uri = Uri.parse(contentUri);
644
                     Uri uri = Uri.parse(contentUri);
638
                     Cursor cursor = appCtx.getContentResolver().query(uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
645
                     Cursor cursor = appCtx.getContentResolver().query(uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
653
                             this.callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, customDest);
660
                             this.callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, customDest);
654
 
661
 
655
                     } catch(Exception ex) {
662
                     } catch(Exception ex) {
656
-                        error = ex.getLocalizedMessage();
663
+                        ex.printStackTrace();
664
+                        this.callback.invoke(ex.getLocalizedMessage(), null);
657
                     }
665
                     }
658
                 }
666
                 }
659
                 else {
667
                 else {