Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-native-fs [Error: Directory could not be created]

I am trying to create a directory using RNFS.mkdir, but it's warning "Directory could not be created"...

"react-native-fs": "^2.18.0",
"react-native": "0.64.0",

The function that I use to write is the following:

const phoneStorageDir = RNFS.ExternalStorageDirectoryPath + '/MyApp/pictures/';

const createAppFolder = async () => {
  if (Platform.OS === 'android') {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
        {
          title: 'Permissions for write access',
          message: 'Give permission to your storage to write a file',
          buttonPositive: 'ok',
        },
      );
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        await RNFS.mkdir(phoneStorageDir);
      } else {
        console.log('permission denied');
        return;
      }
    } catch (err) {
      console.warn(err);
      return;
    }
  }
}

I've also added to it's manifest file:

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

And even added a prop on <application> tag android:requestLegacyExternalStorage="true" as suggested by some other users, while I was searching for this...

Another weird behaviour, is the fact that my app isn't asking for permissions (not even on physical device), although it's granted as soon as I compile the app, as follows:

Sh*ts crazy

like image 899
Mateus FlowinTech Avatar asked May 10 '21 14:05

Mateus FlowinTech


Video Answer


1 Answers

In the library RNFS, the RNFS.ExternalStorageDirectoryPath takes a path that is not allowed on version 11.

Use custom path, just replace ExternalStorageDirectoryPath with /storage/emulated/0/Android/media/{$packagename}

Android for version below 10, your old code will work fine.

like image 177
Manish Arora Avatar answered Oct 30 '22 08:10

Manish Arora