0

I am new to react native I am using react native camera library for capturing the image. I have captured the image now I want it to save on application folder like on assets or src folder. I have the image uri.How I can do that plz let me know.

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
DinhNguyen
  • 303
  • 2
  • 14
Divya Lal
  • 25
  • 1
  • 8

1 Answers1

0

You can use 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);
      });
  });
};
Rajan
  • 1,512
  • 2
  • 14
  • 18