首页 > 解决方案 > React Native 将捕获的图像和视频保存到我设备上的自定义文件夹中

问题描述

我试图为我研究将捕获的图像或视频保存到设备上的自定义文件夹的正确答案,但没有看到合适的答案。我已经能够保存到我的 DCIM,但我不想将它们保存在那里,我想创建一个自定义文件夹来保存从我的应用程序中捕获的图像或视频。我是新来的反应本地人,这是我的学习过程......

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 });

标签: react-nativereact-native-androidreact-native-camera

解决方案


您必须使用 CameraRoll.save 的专辑参数

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

从文档

它允许在提供参数专辑时指定要将资产存储到的特定专辑。在 Android 上,如果没有提供专辑,则使用 DCIM 目录,否则根据提供的类型使用 PICTURE 或 MOVIES 目录。


推荐阅读