react-native-blob-util: Using Android download manager returns wrong path and empty file

When I use Android media manager to download file, and then try to use .path() in copyToMediaStore, it fails.

I investigated it a bit and that’s what I found.

  1. The file is downloaded succesfully and if I tap on a download notification, I can open it. The path of this file is content://com.android.providers.downloads.ui.fileprovider/cache-path/googlelogo_color_272x92dp-2.png. The file is not visible in Photos app and probably remains somewhere in cache.
  2. The fetch result contains path and it’s /data/user/0/com.android.providers.downloads/cache/googlelogo_color_272x92dp-2.png
  3. .base64() of the “fetch” result returns {"_h": 0, "_i": 0, "_j": null, "_k": null} (but this could be not relevant to the issue)
  4. When MediaCollection.copyToMediaStore is called with the .path() mentioned above, it returns No such file '/data/user/0/com.android.providers.downloads/cache/googlelogo_color_272x92dp-2.png'; /data/user/0/com.android.providers.downloads/cache/googlelogo_color_272x92dp-2.png: open failed: ENOENT (No such file or directory)

If I use fetch without addAndroidDownloads option, it works okay and file is saved to media store.

Steps to reproduce:

ReactNativeBlobUtil.config({
  addAndroidDownloads: {
    useDownloadManager: true,
    notification: true, // I know this is by default, just in case
  },
})
  .fetch('GET', 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png')
  .then(async res => {
    const path = res.path();
    console.log('path: ', path);
    console.log('base64: ', await res.base64());
    let result = await ReactNativeBlobUtil.MediaCollection.copyToMediaStore(
      {
        name: 'test_file.png', // name of the file
        parentFolder: '', // subdirectory in the Media Store, e.g. HawkIntech/Files to create a folder HawkIntech with a subfolder Files and save the image within this folder
        mime: 'image/png',
      },
      'Download', // Media Collection to store the file in ("Audio" | "Image" | "Video" | "Download")
      path, // Path to the file being copied in the apps own storage
    );
  });
  • React Native version: 0.71.13
  • react-native-blob-util version: 0.19.1
  • Android: v13, 14 in emulator. Version 11 on a real device

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 3
  • Comments: 16 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Working path for both platform:

    const {
      dirs: { DownloadDir, DocumentDir },
    } = ReactNativeBlobUtil.fs;
    const fileDirPathAndroid = '/storage/emulated/0/Download';
    const aPath = Platform.select({ ios: DocumentDir, android: fileDirPathAndroid });
    const fPath = `${aPath}/${item?.docName}.${extension}`;

I was able to fix this issue by setting the Path to ${ReactNativeBlobUtil.fs.dirs.DownloadDir}/${fileName} even though the documentation stated that The destination which the file will be downloaded, it SHOULD be a location on external storage (DCIMDir). It’s working fine now. Since our app is already released and I don’t want to change the code, do you have any advice? Thanks for a great library! @RonRadtke

Thank you.

If you don’t see any errors or problems just keep it as it is. Otherwise feel free to change somewhen later. Since I think it is a nice feature I will regardless bring it live. Will ease the usage

I’m experiencing the same problem, trying to fetch with useDownloadManager set to true and then attempting to copyToMediaStore - which results in the same error described above by @pid0r-3blan. Did anyone find any solution/workaround?

For 3rd point, recheck the .base64() method, you might be printing a promise, just add an await before it.

My problem is with android download manager unable to handle post method requests for download. It just sends a get request without any body.