反应性:0.61.2 react-native-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: ''});
}
答案 0 :(得分:0)
您使用的是Android吗?如果您使用的是android,则应使用PermissionsAndroid获得请求权限。
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;
}
}