首页 > 解决方案 > 错误:无法拍照 - 预览在 React Native 中暂停

问题描述

我在 react-native 中实现了相机。当我单击 Snap 捕获图像时,它显示错误:无法拍照 - 预览已暂停,在拍照之前恢复它。我的代码如下:

<RNCamera
        ref={ref => {
          this.camera = ref;
        }}
        captureAudio={false}
        style={{flex: 1}}
        type={RNCamera.Constants.Type.back}
        androidCameraPermissionOptions={{
          title: 'Permission to use camera',
          message: 'We need your permission to use your camera',
          buttonPositive: 'Ok',
          buttonNegative: 'Cancel',
        }}>
        <Text onPress={this.takePicture}>Snap</Text>
</RNCamera>

constructor(props) {
    super(props);
    this.state = {
      takingPic: false,
    };
  }
  takePicture = async () => {
    if (this.camera && !this.state.takingPic) {
       
      let options = {
        quality: 0.85,
        base64: true,
      };

      this.setState({takingPic: true});

      try {
        const data = await this.camera.takePictureAsync(options);
        Alert.alert('Success', JSON.stringify(data));
      } catch (err) {
        Alert.alert('Error', 'Failed to take picture: ' + (err.message || err));
        return;
      } finally {
        this.setState({takingPic: false});
      }

    }

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

解决方案


推荐阅读