react-native-fs
react-native-fs copied to clipboard
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) })
same issue if any one have solution then please write here
no one giving solution. no response from library contributor. this issue from 29 days
same here
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
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
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
is there any solution yet?
Hi, @ccksfh ,
Please try this
RNFS.mkdir(${RNFS.DocumentDirectoryPath}/new_dir_name/
).then((result)=>{
console.log('Created');
}).catch((err) => {
console.log('err', err)
});
same problem here too
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?
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
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`
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 :)
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 .
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 have the same issue any one have solved this issue ?
Add this in AndroidManifest.xml
it worked for me
<application android:requestLegacyExternalStorage="true" ...
Reference: https://developer.android.com/reference/android/R.attr#requestLegacyExternalStorage
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
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 ofRNFS.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 ! 😍
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
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 .
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
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