【问题标题】:React native: why cant access camera after setting permission in app.jsonReact Native:为什么在 app.json 中设置权限后无法访问相机
【发布时间】:2022-08-16 12:01:59
【问题描述】:

我正在尝试创建一个应用程序,用户可以在按下按钮时拍照。在 app.json 上设置相机权限之前,它在我的物理设备上工作得很好,但是在 app.json 上设置权限后,它就不起作用了。我仍然收到要求用户许可的弹出窗口,但在允许使用相机后,它不会激活相机。当我再次按下按钮时它仍然不起作用。

应用程序.json

\"plugins\": [
      [
        \"expo-image-picker\",
        {
          \"photosPermission\": \"custom photos permission\",
          \"cameraPermission\": \"Allow $(PRODUCT_NAME) to open the camera\",
          
          \"//\": \"Disables the microphone permission\",
          \"microphonePermission\": false
        }
      ]
    ],
\"android\": {
  \"package\":\"mycamera.myapp\",
  \"versionCode\": 2,
  \"permissions\": [\"CAMERA\",\"READ_EXTERNAL_STORAGE\"],
  \"adaptiveIcon\": {
    \"foregroundImage\": \"./assets/adaptive-icon.png\",
    \"backgroundColor\": \"#FFFFFF\"
  }
}

HomeScreen.js

const [allImage, setAllImage] = React.useState([]);
    const useCamera = async () => {
        const hasPermissions = await cameraPermission();
        if (!hasPermissions) {
            return;
        }
        if(allImage.length < 4){
            let result = await ImagePicker.launchCameraAsync({
                allowsEditing: true,
                quality: 0.5,
            });

            if (!result.cancelled) { 
                const name = result.uri.split(\'/\').pop();
                let match = /\\.(\\w+)$/.exec(name);
                let type = match ? `image/${match[1]}` : `image`;
                let newFile = {
                    uri: result.uri,
                    type: type,
                    name: name
                }
                
                setAllImage(newFile)
                setPickedImage(result.uri)
                if (!pickedImage && allImage.length === 0) {
                    setAllImage([newFile]); 
                    setFileName(\"Receipt 1\")
                }else {
                    setAllImage([...allImage, newFile]); 
                    setFileName(fileName + \", Receipt \" + (allImage.length + 1))  
                }
            }
        }
    };

    标签: javascript react-native android-permissions


    【解决方案1】:

    根据官方文档,对于 expo-image-picker 的 expo 权限,您需要创建 app.config,js / app.json 文件并在那里添加权限。

    详细步骤在主页链接中提供-> https://github.com/expo/expo/tree/sdk-46/packages/expo-image-picker

    您需要添加以下权限

    取自官网图片权限的 app.config.js 示例

    {
    
     "expo": {
       "plugins": [
        [
        "expo-image-picker",
        {
          "photosPermission": "custom photos permission",
          "cameraPermission": "Allow $(PRODUCT_NAME) to open the camera",
          "//": "Disables the microphone permission",
          "microphonePermission": false
         }
        ]
      ]
     }
    }
    

    【讨论】:

    • 谢谢,我可以访问相机了。但是当我尝试将捕获的照片提交到 api 时,我的设备冻结了。在设置权限之前,它工作正常。我更新了我的帖子。
    • 您似乎缺少一些其他所需的权限,例如文件存储权限、位置权限(在相机中使用)等。
    • 我尝试了 android 的文档,它是 READ_EXTERNAL_STORAGE 吗?我试过了,但是提交时应用程序崩溃了。我更新了我的帖子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2020-01-13
    • 1970-01-01
    • 2019-11-05
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    相关资源
    最近更新 更多