Md Javed Akhtar
Md Javed Akhtar

Reputation: 199

Unable to create folder in Android 11 React Native using React Native FS

downloadFile = async (url) => {
   try {
     const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
       {
         title: 'Storage Permission',
         message: 'App needs access to memory to download the file ',
       },
     );
     if (granted === PermissionsAndroid.RESULTS.GRANTED) {
       console.log("Permission Granted")
       let dirs = RNFetchBlob.fs.dirs.MainBundleDir;
       const execute = await RNFS.mkdir(`${dirs}/Jharkhand Update Database`)
       console.log(execute)
       console.log(dirs);
     } else {
      ToastAndroid.showWithGravity(
      'You need to give storage permission to download the file',
      ToastAndroid.SHORT,
      ToastAndroid.BOTTOM,
      );
    }
  } catch (err) {
  console.log('Random ', err);
 }
};

Unable to create a folder in android 11. Tried using DocumentDir & SDCardApplicationDir in place of MainBundleDir as well but of no use. It is important to create a folder as we are storing a list of messages which are being sent by the admin so that the app remains accessible even if the mobile data is switched off.

Upvotes: 1

Views: 2579

Answers (1)

Android 11+ needs the permission MANAGE_EXTERNAL_STORAGE alongside with WRITE_EXTERNAL_STORAGE for any file management to work as itended.

Don't forget to add <uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" /> and android:requestLegacyExternalStorage="true" on your manifest.xml file

Read more about it here and here

Upvotes: 0

Related Questions