rn-fetch-blob: Android: "Permission denied" – no matter what I do

Hi, I am trying to save some base64 data to file, but somehow I can’t get it to work. Everytime I am trying to access external storage (/storage/emulated/0), I get Permission denied from createFile() or writeFile()—I have tried both already. I am also asking for permission beforehand, as you can see in my code snippet. I also added the stuff to AndroidManifest.xml.

The only location that doesn’t raise the Permission denied exception is RNFetchBlob.fs.dirs.DocumentDir, but that is /data/user/0/{bundleId}/files and I am not sure, what that’s translating to. This exact path does not exist on my phone, but I suspect it’s {Internal storage}/Android/data/{bundleId}/files. Even so, after running createFile()/writeFile() “successfully” there’s no file at that location.

Am I missing something crucial here? I’m really out of my depth on this one. I hope someone can help me.

  • the version of installed library and RN project. react-native: v0.63.3 rn-fetch-blob: v0.12.0 Android 10 (Samsung Galaxy S10; One UI v2.5)

  • a sample code snippet/repository is very helpful to spotting the problem.

const permissions = await Permissions.requestMultiple([
  Permissions.PERMISSIONS.ANDROID.READ_EXTERNAL_STORAGE,
  Permissions.PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE,
]);
console.log("permissions:", permissions);
if (
  permissions[Permissions.PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE] ===
    Permissions.RESULTS.GRANTED &&
  permissions[Permissions.PERMISSIONS.ANDROID.READ_EXTERNAL_STORAGE] ===
    Permissions.RESULTS.GRANTED
) {
  try {
    // const createdFilePath = await RNFetchBlob.fs.createFile(path, base64Data, 'base64');
    const createdFilePath = await RNFetchBlob.fs.createFile(
      `${RNFetchBlob.fs.dirs.DownloadDir}/test.txt`,
      "Hello World",
      "utf8"
    );
    if (createdFilePath) {
      console.log("created file path:", createdFilePath);
      return true;
    }

    console.warn("saveToFile() :: File was not successfully saved to dir.");
    return false;
  } catch (e) {
    console.warn(
      "saveToFile() :: RNFetchBlob.fs.createFile raised an exception:",
      e.message
    );
    return false;
  }
} else {
  console.warn("Permission to write to external storage not granted.");
}

If you need any additional info, just let me know.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 3
  • Comments: 23

Most upvoted comments

I got similar issue, I tried to write a file to RNFetchBlob.fs.dirs.DownloadDir but it always returns Permisson Denied. After digging a bit more into it turns out Android 10 introduce a new storage paradigm called scoped storage. So far the only workaround that I found is to add

  <application android:requestLegacyExternalStorage="true" ... >
    ...
  </application>

to your AndroidManifest.xml but this is only temporary. It would be amazing if rn-fetch-blob could support scoped storage 🙇 .

Screenshot 2021-04-22 at 12 54 59 PM

Google won’t allow android:requestLegacyExternalStorage after May 5. This needs to be solved soon

I can currently access the download directory on all Android versions (including 11) with this setup:

build.gradle:

minSdkVersion = 21
compileSdkVersion = 30
targetSdkVersion = 30

AndroidManifest.xml:

  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
...
  <application
      ...
      android:requestLegacyExternalStorage="true">

I DON’T have the permission MANAGE_EXTERNAL_STORAGE.

I can successfully run RNFetchBlob.fs.exists, RNFetchBlob.fs.writeFile and RNFetchBlob.fs.readFile over the Downloads directory (RNFetchBlob.fs.dirs.DownloadDir), however I don’t understand WHY, since the documentation is pretty clear that the MANAGE_EXTERNAL_STORAGE permission is required in order to read and write to the Download directory.

This is what the permissions looks like in the settings: image

Is this ok or will it break at some point or get rejected when submitted in the play store?

@Dror-Bar This Google Play policy refers specifically to apps that target API level 30 and need the MANAGE_EXTERNAL_STORAGE permission (All Files Access). If you don’t use or plan to use this permission, this policy shouldn’t affect you. If you are currently targeting API level 29 and want to use this permission when you update to target API level 30, you will need to comply with this policy. The last date to update App to target Sdk 30 is August 2021. have time till August

for us it threw an error on android 11, with the code in the other repo it worked fine