@@ -558,33 +558,38 @@ public void onReceive(Context context, Intent intent) {
558
558
DownloadManager dm = (DownloadManager ) appCtx .getSystemService (Context .DOWNLOAD_SERVICE );
559
559
dm .query (query );
560
560
Cursor c = dm .query (query );
561
+ String filePath = null ;
562
+ // the file exists in media content database
561
563
if (c .moveToFirst ()) {
562
564
String contentUri = c .getString (c .getColumnIndex (DownloadManager .COLUMN_LOCAL_URI ));
563
565
Uri uri = Uri .parse (contentUri );
564
566
Cursor cursor = appCtx .getContentResolver ().query (uri , new String []{android .provider .MediaStore .Images .ImageColumns .DATA }, null , null , null );
565
567
// use default destination of DownloadManager
566
568
if (cursor != null ) {
567
569
cursor .moveToFirst ();
568
- String filePath = cursor .getString (0 );
570
+ filePath = cursor .getString (0 );
569
571
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 ;
585
575
}
586
576
}
587
577
}
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 );
588
593
}
589
594
}
590
595
}
0 commit comments