瀏覽代碼

Fixing #236 which did not expect null from DownloadManager.query()

Mark Tolmacs 5 年之前
父節點
當前提交
ede920cd13
共有 1 個檔案被更改,包括 30 行新增20 行删除
  1. 30
    20
      android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

+ 30
- 20
android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java 查看文件

@@ -667,29 +667,39 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
667 667
                 DownloadManager dm = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);
668 668
                 dm.query(query);
669 669
                 Cursor c = dm.query(query);
670
-
670
+                // #236 unhandled null check for DownloadManager.query() return value
671
+                if (c == null) {
672
+                    this.callback.invoke("Download manager failed to download from  " + this.url + ". Query was unsuccessful ", null, null);
673
+                    return;
674
+                }
671 675
 
672 676
                 String filePath = null;
673
-                // the file exists in media content database
674
-                if (c.moveToFirst()) {
675
-                    // #297 handle failed request
676
-                    int statusCode = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
677
-                    if(statusCode == DownloadManager.STATUS_FAILED) {
678
-                        this.callback.invoke("Download manager failed to download from  " + this.url + ". Status Code = " + statusCode, null, null);
679
-                        return;
680
-                    }
681
-                    String contentUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
682
-                    if ( contentUri != null &&
683
-                            options.addAndroidDownloads.hasKey("mime") &&
684
-                            options.addAndroidDownloads.getString("mime").contains("image")) {
685
-                        Uri uri = Uri.parse(contentUri);
686
-                        Cursor cursor = appCtx.getContentResolver().query(uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
687
-                        // use default destination of DownloadManager
688
-                        if (cursor != null) {
689
-                            cursor.moveToFirst();
690
-                            filePath = cursor.getString(0);
691
-                            cursor.close();
677
+                try {    
678
+                    // the file exists in media content database
679
+                    if (c.moveToFirst()) {
680
+                        // #297 handle failed request
681
+                        int statusCode = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
682
+                        if(statusCode == DownloadManager.STATUS_FAILED) {
683
+                            this.callback.invoke("Download manager failed to download from  " + this.url + ". Status Code = " + statusCode, null, null);
684
+                            return;
692 685
                         }
686
+                        String contentUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
687
+                        if ( contentUri != null &&
688
+                                options.addAndroidDownloads.hasKey("mime") &&
689
+                                options.addAndroidDownloads.getString("mime").contains("image")) {
690
+                            Uri uri = Uri.parse(contentUri);
691
+                            Cursor cursor = appCtx.getContentResolver().query(uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
692
+                            // use default destination of DownloadManager
693
+                            if (cursor != null) {
694
+                                cursor.moveToFirst();
695
+                                filePath = cursor.getString(0);
696
+                                cursor.close();
697
+                            }
698
+                        }
699
+                    }
700
+                } finally {
701
+                    if (c != null) {
702
+                        c.close();
693 703
                     }
694 704
                 }
695 705