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

Created on 23 Nov 2020  Â·  13Comments  Â·  Source: joltup/rn-fetch-blob

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.

Most helpful comment

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 🙇 .

All 13 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 🙇 .

Got the same Problem, this workaround fixed it for me. Would be great if this will be part of rn-fetch-blob in an next update.

same here

I found a way to get around it:
https://github.com/idealistspace/react-native-android-file-util
Use this instead, to save a file somewhere on the phone
the other fix doesn't work on Andriod 11

@2mt-hillen "fix doesn't work on Andriod 11" -> you mean already now or its only when app is targeting sdk level 30?

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

and we're targeting sdk 29

According to the android 11 release notes, requestLegacyExternalStorage should continue to work on Android 11 devices if we are targetting API level 29.

https://developer.android.com/about/versions/11/privacy/storage

it's not working on Android 11 devices at all anymore

it's not working on Android 11 devices at all anymore

@Tino-F are you sure? did you try requestLegacyExternalStorage as supposed before?

it's not working on Android 11 devices at all anymore

@Tino-F are you sure? did you try requestLegacyExternalStorage as supposed before?

Yeah it looks like Google Thanosd the requestLegacyExternalStorage option. Someone explained it pretty well in the stack overflow link below.

https://stackoverflow.com/questions/63364476/requestlegacyexternalstorage-is-not-working-in-android-11-api-30

it's not working on Android 11 devices at all anymore

@Tino-F are you sure? did you try requestLegacyExternalStorage as supposed before?

Yeah it looks like Google Thanosd the requestLegacyExternalStorage option. Someone explained it pretty well in the stack overflow link below.

https://stackoverflow.com/questions/63364476/requestlegacyexternalstorage-is-not-working-in-android-11-api-30

it depends on targetSdkVersion. It should still work if you are targeting API 29

it's not working on Android 11 devices at all anymore

@Tino-F are you sure? did you try requestLegacyExternalStorage as supposed before?

Yeah it looks like Google Thanosd the requestLegacyExternalStorage option. Someone explained it pretty well in the stack overflow link below.
https://stackoverflow.com/questions/63364476/requestlegacyexternalstorage-is-not-working-in-android-11-api-30

it depends on targetSdkVersion. It should still work if you are targeting API 29

I can confirm, my app is targeting API 29 and requestLegacyExternalStorage=true is still applying on my Samsung S10+ with Android 11.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

learnyst picture learnyst  Â·  27Comments

Evangelione picture Evangelione  Â·  16Comments

fa16mscs0004 picture fa16mscs0004  Â·  13Comments

fgagneten picture fgagneten  Â·  23Comments

baxri picture baxri  Â·  22Comments