react-native-image-pickerlaunchCamera在android中不起作用
我正在使用"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."}
这是我的代码
{"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."}
回答
在捕获图像之前,请向用户询问相机权限。在棉花糖版本以上的 Android 中,您还应该询问运行时权限,这些权限称为危险权限。
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
THE END
二维码