【问题标题】:react-native-image-picker launchCamera in not working in androidreact-native-image-picker launchCamera 在 android 中不起作用
【发布时间】:2020-12-18 12:09:51
【问题描述】:

我正在使用 "react-native-image-picker": "^3.0.1" 在本机反应中捕获图像。但在 android 9 中打开相机时出现错误。

我有错误:

{"errorCode": "others", "errorMessage": "This library does not require Manifest.permission.CAMERA, if you add this permission in manifest then you have to obtain the same."}

这是我的代码

ImagePicker.launchCamera(
          {
            includeBase64: false,
            mediaType: 'photo',
            quality: 0.8,
          },
          async (response) => {
            if (response.didCancel) {
              console.log('User cancelled image picker');
            } else if (response.error) {
              console.log('ImagePicker Error: ', response.error);
            } else {
              
            }
          },
        );

【问题讨论】:

  • 删除 ``` Manifest.permission.CAMERA `` 权限,如在清单中设置的错误消息中所述
  • 我已经尝试过了,但仍然遇到这个问题

标签: android react-native android-camera react-native-image-picker


【解决方案1】:

在拍摄图像之前,向用户询问相机权限。在 marshmallow 版本以上的 Android 中,您还应该询问 Run Time 权限,这称为危险权限。

const requestCameraPermission = async () => {
  try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.CAMERA,
      {
        title: "App Camera Permission",
        message:"App needs access to your camera "
        buttonNeutral: "Ask Me Later",
        buttonNegative: "Cancel",
        buttonPositive: "OK"
      }
    );
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      console.log("Camera permission given");
    } else {
      console.log("Camera permission denied");
    }
  } catch (err) {
    console.warn(err);
  }
};

然后如果在if调用中授予权限

ImagePicker.launchCamera

【讨论】:

    【解决方案2】:
    We need to add run time permissions for the react-native-image-picker . We also need to add seperate permission request for camera and external storage as.please check out below code worked for me .
    
      const grantedcamera = await PermissionsAndroid.request(
            PermissionsAndroid.PERMISSIONS.CAMERA,
            {
              title: "App Camera Permission",
              message:"App needs access to your camera ",
              buttonNeutral: "Ask Me Later",
              buttonNegative: "Cancel",
              buttonPositive: "OK"
            }
          );
          const grantedstorage = await PermissionsAndroid.request(
            PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
            {
              title: "App Camera Permission",
              message:"App needs access to your camera ",
              buttonNeutral: "Ask Me Later",
              buttonNegative: "Cancel",
              buttonPositive: "OK"
            }
          );
          if (grantedcamera === PermissionsAndroid.RESULTS.GRANTED && grantedstorage ===  PermissionsAndroid.RESULTS.GRANTED) {
            console.log("Camera & storage permission given");
              
            var options = {
              mediaType: 'photo', //to allow only photo to select ...no video
              saveToPhotos:true,  //to store captured photo via camera to photos or else it will be stored in temp folders and will get deleted on temp clear
              includeBase64:false,
            };
    
            launchCamera (options, (res) => {
              console.log('Response = ', res);
        
              if (res.didCancel) {
                console.log('User cancelled image picker');
              } else if (res.error) {
                console.log('ImagePicker Error: ', res.error);
              } else if (res.customButton) {
                console.log('User tapped custom button: ', res.customButton);
                alert(res.customButton);
              } else {
               // let source = res;
                // var resourcePath1 = source.assets[0].uri;
                const source = { uri: res.uri };
                console.log('response', JSON.stringify(res));
        
                 setImageSource(source.uri);
               
                
              }
            });
    
    
          } else {
            console.log("Camera permission denied");
          }
    

    【讨论】:

      【解决方案3】:

      这对我有帮助

      将选项添加到 android\app\src\main\AndroidManifest.xml -> 部分 application -> 参数 android:requestLegacyExternalStorage="true"

      android\app\src\main\AndroidManifest.xml

      ...
          <application
            ...
            android:requestLegacyExternalStorage="true"
            ...>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-20
        • 1970-01-01
        • 2022-11-08
        • 1970-01-01
        • 1970-01-01
        • 2020-09-16
        • 1970-01-01
        相关资源
        最近更新 更多