react-native-fs icon indicating copy to clipboard operation
react-native-fs copied to clipboard

Problem with create a new directory (RNFS.mkdir), in API Level 23.

Open ElangoPrince opened this issue 5 years ago • 23 comments

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) })

ElangoPrince avatar May 21 '19 09:05 ElangoPrince

same issue if any one have solution then please write here

yogeshmoradiya121 avatar May 31 '19 05:05 yogeshmoradiya121

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

yogeshmoradiya121 avatar Jun 19 '19 10:06 yogeshmoradiya121

same here

ccksfh avatar Jun 20 '19 07:06 ccksfh

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

hi,Maybe you could try explicitly request the permission of type storage. I solved the issue by this

ccksfh avatar Jun 21 '19 01:06 ccksfh

above api level 23 we have to take explicit storage request i know. add i'm also doing this but error shows in console err Error: Directory could not be created at createErrorFromErrorData (NativeModules.js:155) at NativeModules.js:104 at MessageQueue.__invokeCallback (MessageQueue.js:414) at MessageQueue.js:127 at MessageQueue.__guard (MessageQueue.js:314) at MessageQueue.invokeCallbackAndReturnFlushedQueue (MessageQueue.js:126) at debuggerWorker.js:80

yogeshmoradiya121 avatar Jul 08 '19 05:07 yogeshmoradiya121

above api level 23 we have to take explicit storage request i know. add i'm also doing this but error shows in console err Error: Directory could not be created at createErrorFromErrorData (NativeModules.js:155) at NativeModules.js:104 at MessageQueue.__invokeCallback (MessageQueue.js:414) at MessageQueue.js:127 at MessageQueue.__guard (MessageQueue.js:314) at MessageQueue.invokeCallbackAndReturnFlushedQueue (MessageQueue.js:126) at debuggerWorker.js:80

me too

IseeUDenis avatar Aug 27 '19 05:08 IseeUDenis

is there any solution yet?

valdman40 avatar Sep 12 '19 13:09 valdman40

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

ElangoPrince avatar Jan 03 '20 07:01 ElangoPrince

same problem here too

jackyleehong avatar May 11 '20 03:05 jackyleehong

I have same problem. I want to create a folder for my app in storage like whatsapp and other apps . so I dont want to use DocumentDirectoryPath. is there any solution?

samaneh-kamalian avatar May 19 '20 06:05 samaneh-kamalian

I have same problem. I want to create a folder for my app in storage like whatsapp and other apps . so I dont want to use DocumentDirectoryPath. is there any solution?

Giving storage permission to my app fixed this issue: Settings -> App -> {{MY APP}} -> Permissions -> Storage

samaneh-kamalian avatar May 19 '20 07:05 samaneh-kamalian

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`

lylest avatar May 24 '20 09:05 lylest

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 :)

JoaquimDmt avatar Jun 05 '20 01:06 JoaquimDmt

Guys I Know how to solve it it's very tricky I will grab my computer soon and post a sample code of how I did solved it

On Fri, Jun 5, 2020, 4:05 AM JoaquimDmt [email protected] wrote:

For me it 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 solution yet. So, after many researched 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." 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 !

Documentation here https://openbase.io/js/react-native-fs or here https://www.npmjs.com/package/react-native-fs

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/itinance/react-native-fs/issues/677#issuecomment-639198249, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIFSPYTW4PIOO55CWUW2CWLRVBAGTANCNFSM4HOI4SPQ .

lylest avatar Jun 05 '20 03:06 lylest

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.

Moai- avatar Aug 17 '20 05:08 Moai-

I have the same issue any one have solved this issue ?

azizijunaid-zz avatar Aug 24 '20 15:08 azizijunaid-zz

Add this in AndroidManifest.xml it worked for me

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

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

sajjad26 avatar Aug 31 '20 19:08 sajjad26

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

ofiron01 avatar Oct 22 '20 11:10 ofiron01

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.

I thought for a long time also that it was not possible to write somewhere else in the storage with this package but IT IS POSSIBLE YEEEES !!!! Check lylest answer upper ! 😍

JoaquimDmt avatar Oct 23 '20 13:10 JoaquimDmt

Please correct if I'm wrong. rnfs.mkdir is a file creation method for android only. Are you sure this method works for iOS devices? mkdir only writes for android if I haven't read the documentation wrong. I understood that way

huzeyfetas avatar Apr 12 '21 17:04 huzeyfetas

Check the documentation, it should works on iOS, you juste need to change the directory path according to the environnement iOS or Android.

Le lun. 12 avr. 2021 à 19:51, Hüzeyfe TAŞ @.***> a écrit :

Please correct if I'm wrong. rnfs.mkdir is a file creation method for android only. Are you sure this method works for iOS devices? mkdir only writes for android if I haven't read the documentation wrong. I understood that way

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/itinance/react-native-fs/issues/677#issuecomment-818006331, or unsubscribe https://github.com/notifications/unsubscribe-auth/AL3JVVQRYEKYBTKLD4LFJJ3TIMXJBANCNFSM4HOI4SPQ .

JoaquimDmt avatar Apr 12 '21 17:04 JoaquimDmt

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`

i have add this permission but got same issue, its working fine on android 8 but not working on android 9 and onward image

smismailalam avatar Oct 13 '21 13:10 smismailalam

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.

Original link https://stackoverflow.com/a/70195624/8098613

ahsan-abrar avatar Feb 15 '22 15:02 ahsan-abrar