Skip to content

Commit 9a10735

Browse files
authored
Merge pull request #314 from plukkido/master
Fixing #236 which did not expect null from DownloadManager.query()
2 parents 6bbebd3 + ede920c commit 9a10735

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -656,30 +656,40 @@ public void onReceive(Context context, Intent intent) {
656656
DownloadManager dm = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);
657657
dm.query(query);
658658
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+
}
660664

661665
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+
}
681687
}
682688
}
689+
} finally {
690+
if (c != null) {
691+
c.close();
692+
}
683693
}
684694

685695
// When the file is not found in media content database, check if custom path exists

0 commit comments

Comments
 (0)