加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

react-native – React Native文件处理 – 删除图像

发布时间:2020-12-15 20:12:57 所属栏目:百科 来源:网络整理
导读:我使用 react-native-camera点击图片.我得到了一个文件路径,如:“file:///storage/emulated/0/Pictures/IMG_20161228_021132.jpg”,来自我在相机中存储的相机数据.我能够使用它作为使用Image组件“Image source = {{uri:this.props.note.imagePath.path}}
我使用 react-native-camera点击图片.我得到了一个文件路径,如:“file:///storage/emulated/0/Pictures/IMG_20161228_021132.jpg”,来自我在相机中存储的相机数据.我能够使用它作为使用Image组件“Image source = {{uri:this.props.note.imagePath.path}}”显示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);
}

remote debugging snapshot

解决方法

所以我能够用 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);
      });
  }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读