react-native-vision-camera icon indicating copy to clipboard operation
react-native-vision-camera copied to clipboard

❓ Why I am getting only denied from requestCameraPermission

Open JaLe29 opened this issue 3 years ago • 1 comments

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

JaLe29 avatar Aug 24 '22 08:08 JaLe29

Have you looked at the settings in the phone/emulator you are using?

kicksent avatar Sep 03 '22 18:09 kicksent

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?

CristhianMotoche avatar Dec 08 '22 02:12 CristhianMotoche

We are experiencing the same issue on Android. Permission is always denied and the prompt to allow camera and/or microphone is never shown.

xeroxoid avatar Dec 11 '22 19:12 xeroxoid

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

isaacparsons-dw avatar Jan 11 '23 05:01 isaacparsons-dw

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

Same issue here. Solved using PermissionsAndroid. Thanks to @isaacparsons-dw. We still waiting for an official solution.

buenaondalab avatar Feb 07 '23 22:02 buenaondalab

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]);

sekhuat avatar Mar 07 '23 09:03 sekhuat

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.

sahinbezeng avatar Jul 05 '23 20:07 sahinbezeng

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]);

davidfeldt avatar Aug 09 '23 17:08 davidfeldt

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 💖

mrousavy avatar Sep 30 '23 09:09 mrousavy

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

What about IOS?

manish0707 avatar Oct 02 '23 07:10 manish0707

This has been fixed now btw, Permissions dont return denied on android anymore

mrousavy avatar Oct 03 '23 10:10 mrousavy

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.

This worked for me!

Kata-C avatar Oct 10 '23 20:10 Kata-C