Home > Mobile >  react-native-fs ENOENT: open failed: EACCES (Permission denied), open '/storage/emulated/0/Down
react-native-fs ENOENT: open failed: EACCES (Permission denied), open '/storage/emulated/0/Down

Time:12-14

Hello everyone

Environment :

  • Android
  • react-native 0.68.5
  • react-native-fs 2.20.0

Need:

Save files persistent to the application (if I uninstall the application the files need to persist). If I reinstall the application I need to be able to manipulate these files again. I uploaded the files in the download folder with RNFS.DownloadDirectoryPath so that they are not deleted when the application is uninstalled but the files are public and can be downloaded to another location if needed.

Error:

Everything works perfectly until I uninstall and reinstall the application. As soon as I want to manipulate an existing file in my /storage/emulated/0/Download/AppName/...

I encounter the following error when I want to move file :

Error: ENOENT: open failed: EACCES (Permission denied), open '/storage/emulated/0/Download/AppName/...

I encounter the following error when I want to download file :

Error: ENOENT: no such file or directory, open '/storage/emulated/0/Download

Before manipulating a file I make sure I have the :

  • PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE
  • PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE

My build.gradle :

buildToolsVersion = "31.0.0" minSdkVersion = 30 compileSdkVersion = 31 targetSdkVersion = 31

My manifest :

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

<application
....
android:requestLegacyExternalStorage="true"
android:preserveLegacyExternalStorage="true"
...

A big thank you to the people who will take their time to help me

To try to solve the problem I added this line in my manifest :

android:preserveLegacyExternalStorage="true"

and I changed the minSdkVersion in my build.gradle from 21 to 30.

I have the impression that the rights belonged to the application as if by reinstalling the application it lost its rights.

CodePudding user response:

For android, you have to take permission from the user for that you have to write this in your react-native code while communicating with storage

import {Button, PermissionsAndroid} from "react-native";

const requestCameraPermission = async () => {
try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
      {
        title: "Cool Photo App Camera Permission",
        message:
          "Your app needs permission.",
        buttonNeutral: "Ask Me Later",
        buttonNegative: "Cancel",
        buttonPositive: "OK"
      }
    );
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      return true;
    } else {
      console.log("Camera permission denied");
      return false;
    }
  } catch (err) {
    console.warn(err);
    return false;
  }
}

call above function on button press or anywhere else in your code

CodePudding user response:

You did not tell it but this happens to you on Android 11 devices.

On Android 11 an app has only access to the files it created itself and media files in public directories.

After reinstall an app is considered a different app.

Use Storage Access Framework ACTION_OPEN_DOCUMENT to let the user pick the files.

  • Related