我使用 react-native-camera点击图片.我得到了一个文件路径,如:“file:///storage/emulated/0/Pictures/IMG_20161228_021132.jpg”,来自我在相机中存储的相机数据.我能够使用它作为使用Image组件“Image
现在我想添加删除图像功能.有人可以建议如何使用上述路径在手机中访问此图像并将其从手机中删除.
我检查了react-native-filesystem,但是当我使用checkIfFileExists函数传递这个路径时,我得知该文件不存在.不确定出了什么问题.
async checkIfFileExists(path) {
const fileExists = await FileSystem.fileExists(path);
//const directoryExists = await FileSystem.directoryExists('my-directory/my-file.txt');
console.log(`file exists: ${fileExists}`);
//console.log(`directory exists: ${directoryExists}`);
}
deleteNoteImage (note) {
console.log(note.imagePath.path);
//check if file exists
this.checkIfFileExists(note.imagePath.path);
//console.log();
note.imagePath = null;
this.updateNote(note);
}
所以我能够用
react-native-fs做到这一点
路径需要声明如下:
var RNFS = require('react-native-fs');
const dirPicutures = `${RNFS.ExternalStorageDirectoryPath}/Pictures`;
然后此功能删除给定图像名称的图像.
deleteImageFile(filename) {
const filepath = `${dirPicuturesTest}/${filename}`;
RNFS.exists(filepath)
.then( (result) => {
console.log("file exists: ", result);
if(result){
return RNFS.unlink(filepath)
.then(() => {
console.log('FILE DELETED');
})
// `unlink` will throw an error, if the item to unlink does not exist
.catch((err) => {
console.log(err.message);
});
}
})
.catch((err) => {
console.log(err.message);
});
}
