我正在尝试在我的本机应用中添加运行时权限功能但无法添加。请帮我。这是我的代码。我已经尝试了所有的方法,但它没有用。
samplefunction=()=>{
async function requestCameraPermission() {
try {
const granted = PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA,
{
'title': 'Cool Photo App Camera Permission',
'message': 'Cool Photo App needs access to your camera ' +
'so you can take awesome pictures.'
}
)
if (granted === true || granted === PermissionsAndroid.RESULTS.GRANTED){
console.log("You can use the camera")
Alert.alert("You can use the camera")
} else {
console.log("Camera permission denied")
Alert.alert("Camera permission denied")
}
} catch (err) {
console.warn(err)
}
}
requestCameraPermission(false);
}
答案 0 :(得分:0)
权限机制是异步的,并返回一个promise。你应该await
结果:
const granted = await PermissionsAndroid.request( ...
答案 1 :(得分:0)
在使用PermissionAndroid功能之前,请确保已在AndroidManifest.xml中添加此行:
<uses-permission android:name="android.permission.CAMERA" />