【问题标题】:how to save captured image to application folder in react native?如何在本机反应中将捕获的图像保存到应用程序文件夹?
【发布时间】:2020-03-26 13:16:29
【问题描述】:

我是 react native 的新手我正在使用 react native 相机库来捕获图像。我现在已经捕获了图像我希望它保存在应用程序文件夹中,例如资产或 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-native react-native-camera


    【解决方案1】:

    您可以使用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);
          });
      });
    };
    

    【讨论】:

      猜你喜欢
      • 2021-05-06
      • 1970-01-01
      • 1970-01-01
      • 2018-04-01
      • 2015-04-14
      • 1970-01-01
      • 2018-05-30
      • 2021-06-15
      • 2014-06-13
      相关资源
      最近更新 更多