diff --git a/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java b/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java index 369b9baaf..ef3b54f51 100644 --- a/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java +++ b/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java @@ -667,29 +667,39 @@ public void onReceive(Context context, Intent intent) { DownloadManager dm = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE); dm.query(query); Cursor c = dm.query(query); - + // #236 unhandled null check for DownloadManager.query() return value + if (c == null) { + this.callback.invoke("Download manager failed to download from " + this.url + ". Query was unsuccessful ", null, null); + return; + } String filePath = null; - // the file exists in media content database - if (c.moveToFirst()) { - // #297 handle failed request - int statusCode = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); - if(statusCode == DownloadManager.STATUS_FAILED) { - this.callback.invoke("Download manager failed to download from " + this.url + ". Status Code = " + statusCode, null, null); - return; - } - String contentUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); - if ( contentUri != null && - options.addAndroidDownloads.hasKey("mime") && - options.addAndroidDownloads.getString("mime").contains("image")) { - Uri uri = Uri.parse(contentUri); - Cursor cursor = appCtx.getContentResolver().query(uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null); - // use default destination of DownloadManager - if (cursor != null) { - cursor.moveToFirst(); - filePath = cursor.getString(0); - cursor.close(); + try { + // the file exists in media content database + if (c.moveToFirst()) { + // #297 handle failed request + int statusCode = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); + if(statusCode == DownloadManager.STATUS_FAILED) { + this.callback.invoke("Download manager failed to download from " + this.url + ". Status Code = " + statusCode, null, null); + return; } + String contentUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); + if ( contentUri != null && + options.addAndroidDownloads.hasKey("mime") && + options.addAndroidDownloads.getString("mime").contains("image")) { + Uri uri = Uri.parse(contentUri); + Cursor cursor = appCtx.getContentResolver().query(uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null); + // use default destination of DownloadManager + if (cursor != null) { + cursor.moveToFirst(); + filePath = cursor.getString(0); + cursor.close(); + } + } + } + } finally { + if (c != null) { + c.close(); } }