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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With