Skip to content

Commit ede920c

Browse files
committed
Fixing #236 which did not expect null from DownloadManager.query()
1 parent d8c79f6 commit ede920c

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
@@ -667,29 +667,39 @@ public void onReceive(Context context, Intent intent) {
667667
DownloadManager dm = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);
668668
dm.query(query);
669669
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+
}
671675

672676
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;
692685
}
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();
693703
}
694704
}
695705

0 commit comments

Comments
 (0)