react-native-fs: Problem with create a new directory (RNFS.mkdir), in API Level 23.

react-native-fs can’t create a new directory Android API level 23 and higher. but 22 is working fine. got error: err: { [Error: Directory could not be created] framesToPop: 1, code: ‘EUNSPECIFIED’ } here my code 👍 ``` const currentDirectory = ‘MyFile’; const absolutePath = /storage/emulated/0/${currentDirectory}

RNFS.mkdir(absolutePath).then((result) => { console.log(‘result’, result) }).catch((err) => { console.log(‘err’, err) })

About this issue

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

Most upvoted comments

I Solved it by first adding these lines into androidManifest so as to attain persmiision to write in storage <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

then i requested persmission in my function before writing

` saveOffline = async() =>{

try {
  const granted = await PermissionsAndroid.request(
    PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
    {
      title: "Permission",
      message: "swahiliPodcast needs to read storage "
    }
  );
  if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        //DO SOMETHING
           // get a list of files and directories in the main bundle
    RNFS.readDir(RNFS.ExternalStorageDirectoryPath ) // On Android, use "RNFS.DocumentDirectoryPath" (MainBundlePath is not defined)
                    .then((result) => {
                 //
                 let appFolder = 'swahiliPodcast'
                 let path = RNFS.ExternalStorageDirectoryPath+'/'+appFolder;
                 RNFS.mkdir(path).catch((error)=>{console.log(error)})
           
             }).catch((err) => {
           console.log(err.message, err.code);
            });
    
        //END OF DOING SOMETHINGS

  } else {
    console.log(
      "Permission Denied!",
      "You need to give  permission to see contacts"
    );
  }
} catch (err) {
 console.log(err);
}

}//saveoffline function ends here`

Add this in AndroidManifest.xml it worked for me

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

Reference: https://developer.android.com/reference/android/R.attr#requestLegacyExternalStorage

For me RNFS.mkdir was working when I used RNFS.mkdir(${RNFS.ExternalStorageDirectoryPath}).then(... , but as soon as I changed for the new directory path that I want : ${RNFS.ExternalStorageDirectoryPath}/myAppFolder/, it was always throwing to me the error (Directory could not be created). I have this issue for months now and still no answer. Many people has the same issue but nobody has reply a correct solution yet. So, after many research I found something in documentation that might explain everything : “Usage (Android) : Android support is currently limited to only the DocumentDirectory. This maps to the app’s files directory.” -Documentation : https://www.npmjs.com/package/react-native-fs or https://openbase.io/js/react-native-fs It seems that for now it is not possible to create our own directories for Android with react-native-fs mkdir. If anyone find a solution someday, please share it !

[EDIT] IT FINALLY WORKS !!! Check lylest answer just before ! You also need to import { PermissionsAndroid } from ‘react-native’ at the top of your file 😃

Hi, @ccksfh , Please try this RNFS.mkdir(${RNFS.DocumentDirectoryPath}/new_dir_name/).then((result)=>{ console.log(‘Created’); }).catch((err) => { console.log(‘err’, err) });

I Solved it by first adding these lines into androidManifest so as to attain persmiision to write in storage <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

then i requested persmission in my function before writing

` saveOffline = async() =>{

try {
  const granted = await PermissionsAndroid.request(
    PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
    {
      title: "Permission",
      message: "swahiliPodcast needs to read storage "
    }
  );
  if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        //DO SOMETHING
           // get a list of files and directories in the main bundle
    RNFS.readDir(RNFS.ExternalStorageDirectoryPath ) // On Android, use "RNFS.DocumentDirectoryPath" (MainBundlePath is not defined)
                    .then((result) => {
                 //
                 let appFolder = 'swahiliPodcast'
                 let path = RNFS.ExternalStorageDirectoryPath+'/'+appFolder;
                 RNFS.mkdir(path).catch((error)=>{console.log(error)})
           
             }).catch((err) => {
           console.log(err.message, err.code);
            });
    
        //END OF DOING SOMETHINGS

  } else {
    console.log(
      "Permission Denied!",
      "You need to give  permission to see contacts"
    );
  }
} catch (err) {
 console.log(err);
}

}//saveoffline function ends here`

This is the solution! thank you

no one giving solution. no response from library contributor. this issue from 29 days

For me RNFS.mkdir was working when I used RNFS.mkdir(${RNFS.ExternalStorageDirectoryPath}).then(... , but as soon as I changed for the new directory path that I want : ${RNFS.ExternalStorageDirectoryPath}/myAppFolder/, it was always throwing to me the error (Directory could not be created). I have this issue for months now and still no answer. Many people has the same issue but nobody has reply a correct solution yet. So, after many research I found something in documentation that might explain everything : “Usage (Android) : Android support is currently limited to only the DocumentDirectory. This maps to the app’s files directory.” -Documentation : https://www.npmjs.com/package/react-native-fs or https://openbase.io/js/react-native-fs It seems that for now it is not possible to create our own directories for Android with react-native-fs mkdir. If anyone find a solution someday, please share it !

This was the big one for me. If your mkdir is mysteriously failing, check to make sure that you are writing at the root of RNFS.DocumentDirectoryPath. RNFS cannot write anywhere else.