react-nativereact-native-camera

React Native Save captured images and video to custom folder on my device


I have tried to research for the right answer for me on saving captured images or videos to custom folder on device but have not seen a suitable answers. I have been able to save to my DCIM, but I don't want to save them there, I want to create a custom folder to save my captured images or video from my app. I am new to react native and this is my learning process...

takePicture = async () => {
        if (this.camera) {
          if (Platform.OS === 'android') {
            await this.checkAndroidPermission();
          }
            const options = { quality: 1 };
            const data = await this.camera.takePictureAsync(options);
            //save photo
            CameraRoll.save(data.uri, 'photo').then(onfulfilled => {
                ToastAndroid.show(`VidApp Photos: ${onfulfilled}`, ToastAndroid.SHORT);
            }).catch(error => {
                ToastAndroid.show(`${error.message}`, ToastAndroid.SHORT);
            });
        }
    };


    recordVideo = async () => {
      if (this.camera) {
          if (!this.state.recording)
              this.startRecording();
          else this.stopRecording();
      }
  }

  startRecording = async () => {
    this.setState({ recording: true });
    this.countRecordTime = setInterval(() => this.setState({ seconds: this.state.seconds + 1 }), 1000);
    const cameraConfig = { maxDuration: this.state.maxDuration };
    const data = await this.camera.recordAsync(cameraConfig);
    this.setState({ recording: false });
    CameraRoll.save(data.uri, 'video').then(onfulfilled => {
        ToastAndroid.show(`VidApp Videos: ${onfulfilled}`, ToastAndroid.SHORT)
    }).catch(error => ToastAndroid.show(`${error.message}`, ToastAndroid.SHORT));
}

stopRecording = () => {
    this.camera.stopRecording();
    clearInterval(this.countRecordTime);
    this.setState({ seconds: 0 });


Solution

  • You have to use the album parameter of CameraRoll.save

    CameraRoll.save(data.uri, {type:'photo',album:'CustomFolder'});
    

    from the docs

    It allows to specify a particular album you want to store the asset to when the param album is provided. On Android, if no album is provided, DCIM directory is used, otherwise PICTURE or MOVIES directory is used depending on the type provided.