反应本机和rn-fetch-blob将捕获的图像从图片移动到在Android中创建的自定义文件夹

时间:2018-12-18 11:16:23

标签: android reactjs react-native

React Native是Jquery的新功能。

我正在创建一个应用程序,用户将在其中拍摄文档照片并保存到服务器。

我正在使用react-native-camera-kit创建一个CameraScreen,它为我提供了图像URI,如下所示

  

/storage/emulated/0/Pictures/IMAGENO1235.jpg

相机屏幕代码

 <CameraKitCameraScreen
            actions={{ leftButtonText: 'Cancel' }}
            onBottomButtonPressed={event => this.onBottomButtonPressed(event)}

            flashImages={{
              on: require('./img/flashon.png'),
              off: require('./img/flashoff.png'),
              auto: require('./img/flashauto.png'),
            }}
            TopTitle={this.state.title}
            cameraFlipImage={require('./img/flip-camera.png')}
            captureButtonImage={require('./img/capture.png')}
            cameraOptions={{
              flashMode: 'auto',             // on/off/auto(default)
              focusMode: 'on',               // off/on(default)
              zoomMode: 'on',                // off/on(default)
              ratioOverlay:'16:9',
              ratioOverlayColor: '#00000077'
            }}
          />

我希望此图像使用以下代码移动到rn-fetch-blob创建的另一个文件夹中

 onBottomButtonPressed(event) {
   if (event.type) {
     if (event.type == "capture") {
      const pictureFolder = RNFetchBlob.fs.dirs.SDCardDir+'/FOSApp/';
      const captureImageLength = event.captureImages.length;
      RNFetchBlob.fs.exists(pictureFolder).then((exists)=>{
        if(exists){
          RNFetchBlob.fs.isDir(pictureFolder).then((isDir)=>{
            if(isDir){

              RNFetchBlob.fs.mv('file:/'+event.captureImages[0].uri, pictureFolder).then(() => {
                  alert('Image Moved');
              }).catch((e)=>{      alert("FAILED:= "+e.message) });
            }else{
              alert('Some Error Happened');
            }
          }).catch((e)=>{ alert("Checking Directory Error : "+e.message); });
        }else{
          RNFetchBlob.fs.mkdir(pictureFolder).then(()=>{
              alert('DIRECTORY CREATED');
          }).catch((e)=>{ alert("Directory Creating Error : "+e.message); });
        }
      });

但这给我错误

  

路径/storage/emulated/0/Pictures/IMAGENO1235.jpg上的源文件不存在。

请帮助。

谢谢

2 个答案:

答案 0 :(得分:1)

我首先创建了一个空文件,然后根据您要实现的目标移动或复制该文件!

async onBottomButtonPressed(event) {
    if (!event.type || event.type != "capture") { return false }
    const pictureFolder = RNFetchBlob.fs.dirs.SDCardDir + '/FOSApp/';
    const captureImageLength = event.captureImages.length;
    let exists = await RNFetchBlob.fs.exists(pictureFolder);

    if (exists) {
        let isDir = await RNFetchBlob.fs.isDir(pictureFolder);
        if (isDir) {
            let filename = event.captureImages[0].uri.split('/')[event.captureImages[0].uri.split('/').length - 1];

            // create an empty file fisrt
            let createEmptyFile = await RNFetchBlob.fs.createFile(pictureFolder + filename, '', 'base64');

            // then you can copy it or move it like
            let _copy = await RNFetchBlob.fs.cp(event.captureImages[0].uri, pictureFolder + filename);
        }
    } else {
        RNFetchBlob.fs.mkdir(pictureFolder).then(() => {
            alert('DIRECTORY CREATED');
        }).catch((e) => { alert("Directory Creating Error : " + e.message); });
    }
}

答案 1 :(得分:0)

更改
RNFetchBlob.fs.mv('file:/'+event.captureImages[0].uri, pictureFolder).then(() => {RNFetchBlob.fs.mv(event.captureImages[0].uri, pictureFolder).then(() => {

目录已创建并且图像已移动,警报显示,但是在已创建的FOSApp目录中没有图像正在移动。