ソースを参照

Merge pull request #314 from plukkido/master

Fixing #236 which did not expect null from DownloadManager.query()
Travis Nuttall 5 年 前
コミット
9a10735758
No account linked to committer's email address
共有1 個のファイルを変更した30 個の追加20 個の削除を含む
  1. 30
    20
      android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

+ 30
- 20
android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java ファイルの表示

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