0

反应原生:0.61.2 反应原生 fs:^2.16.2

我想在外部存储中创建一个新目录。我预计“可能的未处理承诺拒绝(id:2):错误:无法创建目录”

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: ''});
  }
4

1 回答 1

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;
      }
}
于 2019-12-20T13:34:29.080 回答