question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android: "Permission denied" – no matter what I do

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:23

github_iconTop GitHub Comments

16reactions
Mousserlanecommented, Nov 23, 2020

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

4reactions
anjalsaneencommented, Apr 22, 2021
Screenshot 2021-04-22 at 12 54 59 PM

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

ADB: Permission denied...no matter what i do!!! - XDA Forums
' That should do fix it for you. If it does not, go to Control Panel > System > Advanced > Environment Variables....
Read more >
Android: adb: Permission Denied - Stack Overflow
The reason for "permission denied" is because your Android machine has not been correctly rooted. Did you see $ after you started adb...
Read more >
Request app permissions - Android Developers
If the user denies a permission request, your app should help users understand the implications of denying the permission. In particular, your app...
Read more >
Windows 10 "Access Denied" Folder Errors: 5 Easy Fixes
"You have been denied permission to access this folder" is another form of this same error. If the standard steps don't fix it,...
Read more >
Access Denied When Copying Files Windows 10/8/7 - EaseUS
Before you are going to solve this problem, find out what causes this error will do you a favor. Folder ownership has changed....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found