react-native-vision-camera
react-native-vision-camera copied to clipboard
❓ Why I am getting only denied from requestCameraPermission
Question
Hi,
(this is my first contact with react native).
I am stuck on camera setup. I am getting always denied response from await Camera.requestCameraPermission() I dont know why? I never clicked on decline button.
I tested on android emulator, my real device.
Full code:
const getRequestCameraPermission = async () => {
console.log('CLICK')
const newCameraPermission = await Camera.requestCameraPermission();
ToastAndroid.show(newCameraPermission, ToastAndroid.SHORT);
}
This function is fired after click on button.
What I tried
google issues SO
VisionCamera Version
2.14.1
Additional information
- [X] I am using Expo
- [X] I have read the Troubleshooting Guide
- [X] I agree to follow this project's Code of Conduct
- [X] I searched for similar questions in the issues page as well as in the discussions page and found none.
Have you looked at the settings in the phone/emulator you are using?
Hi! I'm experiencing the same issue on version 2.15.2. I believe it's because of these lines here:
PackageManager.PERMISSION_DENIED -> "denied"
PackageManager.PERMISSION_GRANTED -> "authorized"
else -> "not-determined"
So, it seems when the status is not PackageManager.PERMISSION_DENIED or PackageManager.PERMISSION_GRANTED then the result is not-determined and status is obtained from ContextCompat.checkSelfPermission which accordingly to the Kotlin Doc can be only PackageManager.PERMISSION_DENIED or PackageManager.PERMISSION_GRANTED. Then, when would it be not-determined?
We are experiencing the same issue on Android. Permission is always denied and the prompt to allow camera and/or microphone is never shown.
I too had this issue. Permission was always 'denied' even after a fresh instance of the app installed onto a physical device. No prompt either - just denies straight away.
In the end i've had to request the permission manually through PermissonsAndroid from React Native.
Here is a helper function to request permissons with Android:
import { PermissionsAndroid } from 'react-native';
const requestAndroidCameraPermission = useCallback(async () => {
try {
const permissionGranted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.CAMERA, {
title: '<your title here>',
message:
'<your message here>',
buttonNegative: 'Deny',
buttonPositive: 'Allow',
});
// then access permission status
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
// permissons have been accepted - update a useState() here or whatever your usecase is :)
}
} catch (err) {
console.warn(err);
}
}, []);
Conditionally fire above function based on OS... in case anyone needs it
if (Platform.OS === 'android') {
requestAndroidCameraPermission()
}
Hopefully the above is helpful
I too had this issue. Permission was always 'denied' even after a fresh instance of the app installed onto a physical device. No prompt either - just denies straight away.
In the end i've had to request the permission manually through
PermissonsAndroidfrom React Native.Here is a helper function to request permissons with Android:
import { PermissionsAndroid } from 'react-native'; const requestAndroidCameraPermission = useCallback(async () => { try { const permissionGranted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.CAMERA, { title: '<your title here>', message: '<your message here>', buttonNegative: 'Deny', buttonPositive: 'Allow', }); // then access permission status if (granted === PermissionsAndroid.RESULTS.GRANTED) { // permissons have been accepted - update a useState() here or whatever your usecase is :) } } catch (err) { console.warn(err); } }, []);Conditionally fire above function based on OS... in case anyone needs it
if (Platform.OS === 'android') { requestAndroidCameraPermission() }Hopefully the above is helpful
Same issue here. Solved using PermissionsAndroid. Thanks to @isaacparsons-dw.
We still waiting for an official solution.
I guessed the proper way is to use the requestCameraPermission method from the module itself.
const devices = useCameraDevices();
const device = devices.back;
React.useEffect(() => {
if(device) {
Camera.requestCameraPermission()
.then(result => {
console.log('requestCameraPermission: ', result);
})
.catch(err => {
console.log('requestCameraPermission Err: ', err);
});
}
}, [device]);
Hey guys,
Probably you haven't added the permission to AndroidManifest.xml
Same happened to me so i just added the line
<uses-permission android:name="android.permission.CAMERA" /> to AndroidManifest.xml
and problem solved.
I only had this issue on Android when requesting more than one permission. For recording video (use of camera) and audio, there is only the prompt for camera permission and there is no audio permission prompt.
My solution was to use the following:
import {PermissionsAndroid} from 'react-native';
const getAllPermissions = useCallback(async () => {
PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.CAMERA,
PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,
]).then(result => {
if (
result['android.permission.CAMERA'] === 'granted' &&
result['android.permission.RECORD_AUDIO'] === 'granted'
) {
setLoading(devices);
} else {
Alert.alert(
'Permission Not Granted',
'You need to grant this app permission to use the camera to record audio and video',
[
{
text: 'Cancel',
onPress: () => navigation.pop(),
},
{
text: 'Grant',
onPress: () => Linking.openSettings(),
},
],
);
setLoading(null);
}
});
}, [navigation, devices]);
useEffect(() => {
getAllPermissions();
}, [getAllPermissions, devices]);
Closing as this is a stale issue - this might have been fixed with the full rewrite in VisionCamera V3 (🥳) - if not, please create a new issue.
If your issue has been fixed, consider sponsoring me on GitHub to say thanks 💖
I too had this issue. Permission was always 'denied' even after a fresh instance of the app installed onto a physical device. No prompt either - just denies straight away.
In the end i've had to request the permission manually through
PermissonsAndroidfrom React Native.Here is a helper function to request permissons with Android:
import { PermissionsAndroid } from 'react-native'; const requestAndroidCameraPermission = useCallback(async () => { try { const permissionGranted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.CAMERA, { title: '<your title here>', message: '<your message here>', buttonNegative: 'Deny', buttonPositive: 'Allow', }); // then access permission status if (granted === PermissionsAndroid.RESULTS.GRANTED) { // permissons have been accepted - update a useState() here or whatever your usecase is :) } } catch (err) { console.warn(err); } }, []);Conditionally fire above function based on OS... in case anyone needs it
if (Platform.OS === 'android') { requestAndroidCameraPermission() }Hopefully the above is helpful
What about IOS?
This has been fixed now btw, Permissions dont return denied on android anymore
Hey guys,
Probably you haven't added the permission to
AndroidManifest.xmlSame happened to me so i just added the line
<uses-permission android:name="android.permission.CAMERA" />toAndroidManifest.xmland problem solved.
This worked for me!