@@ -558,33 +558,38 @@ public void onReceive(Context context, Intent intent) {
558558 DownloadManager dm = (DownloadManager ) appCtx .getSystemService (Context .DOWNLOAD_SERVICE );
559559 dm .query (query );
560560 Cursor c = dm .query (query );
561+ String filePath = null ;
562+ // the file exists in media content database
561563 if (c .moveToFirst ()) {
562564 String contentUri = c .getString (c .getColumnIndex (DownloadManager .COLUMN_LOCAL_URI ));
563565 Uri uri = Uri .parse (contentUri );
564566 Cursor cursor = appCtx .getContentResolver ().query (uri , new String []{android .provider .MediaStore .Images .ImageColumns .DATA }, null , null , null );
565567 // use default destination of DownloadManager
566568 if (cursor != null ) {
567569 cursor .moveToFirst ();
568- String filePath = cursor .getString (0 );
570+ filePath = cursor .getString (0 );
569571 cursor .close ();
570- this .callback .invoke (null , RNFetchBlobConst .RNFB_RESPONSE_PATH , filePath );
571- }
572- // custom destination
573- else {
574- if (options .addAndroidDownloads .hasKey ("path" )) {
575- try {
576- String customDest = options .addAndroidDownloads .getString ("path" );
577- boolean exists = new File (customDest ).exists ();
578- if (!exists )
579- throw new Exception ("Download manager download failed, the file does not downloaded to destination." );
580- callback .invoke (null , RNFetchBlobConst .RNFB_RESPONSE_PATH , customDest );
581-
582- } catch (Exception ex ) {
583- this .callback .invoke (ex .getLocalizedMessage (), null , null );
584- }
572+ if (filePath != null ) {
573+ this .callback .invoke (null , RNFetchBlobConst .RNFB_RESPONSE_PATH , filePath );
574+ return ;
585575 }
586576 }
587577 }
578+ // When the file is not found in media content database, check if custom path exists
579+ if (options .addAndroidDownloads .hasKey ("path" )) {
580+ try {
581+ String customDest = options .addAndroidDownloads .getString ("path" );
582+ boolean exists = new File (customDest ).exists ();
583+ if (!exists )
584+ throw new Exception ("Download manager download failed, the file does not downloaded to destination." );
585+ callback .invoke (null , RNFetchBlobConst .RNFB_RESPONSE_PATH , customDest );
586+
587+ } catch (Exception ex ) {
588+ this .callback .invoke (ex .getLocalizedMessage (), null , null );
589+ }
590+ }
591+ else
592+ this .callback .invoke (null , RNFetchBlobConst .RNFB_RESPONSE_PATH , filePath );
588593 }
589594 }
590595 }
0 commit comments