世博会缺少相机胶卷许可

Missing camera roll permission in expo

我有 "Change Image" 按钮,我想使用相机胶卷来更改图像。
但是我收到警告说我没有使用相机胶卷的权限。
如何检查是否授予权限?如果不是,我想请求许可。

这是我现在的代码:

    _pickImage = async () => {
        let result = await ImagePicker.launchImageLibraryAsync({
          allowsEditing: true,
          aspect: [4, 3],
        });
    
        console.log(result);
    
        if (!result.cancelled) {
          this.setState({ image: result.uri });
        }
    };

这可能是一个非常愚蠢的问题,但我在这里有点困惑...

如果您需要更多信息,请发表评论。

谢谢!

如果您正在使用 Expo,您可以从 Expo 获得 Permission。遵循他们的文档,太棒了!

看起来像这样:

import * as Permissions from 'expo-permissions';

async componentDidMount() {
  const permission = await Permissions.getAsync(Permissions.CAMERA_ROLL);
  if (permission.status !== 'granted') {
      const newPermission = await Permissions.askAsync(Permissions.CAMERA_ROLL);
      if (newPermission.status === 'granted') {
        //its granted.
      }
  } else {
   ....your code
  }
}

Link 到 Expo

我用一些新代码进行了编辑。您可以完全使用 askAsync,由您决定。这些文档非常有用!