0

react-native: 0.61.2 react-native-fs: ^2.16.2

I want to create a new directory in external storage. I expected "Possible Unhandled Promise Rejection (id: 2): Error: Directory could not be created"

export function createDirCategory(fileName) {
  const file = `${dirHome}/${fileName}`;
    return new Promise((resolve, reject) => {   
    RNFS.mkdir(file)
      .then(() => {
        resolve(true);
      })
      .catch(error => {
        reject(error);
      });
  });
};

addCategorty() {
    const {categoryName} = this.state;
    createDirCategory(categoryName);
    const folderUrl =getDirUrl(categoryName);
    let id =
      Math.random()
        .toString(36)
        .substring(2) + Date.now().toString(36);
    this.props.changeState;
    store.push('categories', {
      id,
      categoryName,
      imageUrl: [],
      folderUrl,
    });
    this.setState({categoryName: ''});
  }
görkem
  • 25
  • 7

1 Answers1

0

Are you using android? If you are using android then you should use PermissionsAndroid for request permission.

import {PermissionsAndroid} from 'react-native'
...
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) {
          console.log('You can use the storage');
        } else {
          console.log('permission denied');
          return;
        }
      } catch (err) {
        console.warn(err);
        return;
      }
}
DevLover
  • 775
  • 6
  • 24