首页 > 解决方案 > 如何在本机反应中将捕获的图像保存到应用程序文件夹?

问题描述

我是反应原生的新手我正在使用反应原生相机库来捕获图像。我现在已经捕获了图像我希望它保存在应用程序文件夹中,例如资产或 src 文件夹。我有图像 uri。我该怎么做请让我知道。

state = {

    cameraType : 'back',
    mirrorMode : false,
    path:null
 }
<View>
 <RNCamera
          ref={ref => {
            this.camera = ref;
          }}

          mirrorImage={this.state.mirrorMode}
          style={styles.preview}
          type={RNCamera.Constants.Type.front}
          flashMode={RNCamera.Constants.FlashMode.on}
          androidCameraPermissionOptions={{
            title: 'Permission to use camera',
            message: 'We need your permission to use your camera',
            buttonPositive: 'Ok',
            buttonNegative: 'Cancel',
          }}
          androidRecordAudioPermissionOptions={{
            title: 'Permission to use audio recording',
            message: 'We need your permission to use your audio',
            buttonPositive: 'Ok',
            buttonNegative: 'Cancel',
          }}

        />
<Button onPress={() => this.takePicture()} style={styles.btncontainer}>


           <Text>Take Picture</Text>
           </Button>

      </View>

 takePicture = async() => {
  if (this.camera) {
    const options = { quality: 0.5, base64: true };
    const data = await this.camera.takePictureAsync(options);
    this.setState({ path: data.uri });
    console.log("url",this.state.path)
  }
};


console.log url file:///data/user/0/com.tracking/cache/Camera/231f7d30-f74c-4f6d-b955-284b108592ca.jpg

标签: react-nativereact-native-camera

解决方案


您可以使用react-native-fs

const moveAttachment = async (filePath, newFilepath) => {
  return new Promise((resolve, reject) => {
    RNFS.mkdir(dirPicutures)
      .then(() => {
        RNFS.moveFile(filePath, newFilepath)
          .then(() => {
            console.log('FILE MOVED', filePath, newFilepath);
            resolve(true);
          })
          .catch(error => {
            console.log('moveFile error', error);
            reject(error);
          });
      }) 
      .catch(err => {
        console.log('mkdir error', err);
        reject(err);
      });
  });
};

推荐阅读