Gautham Raj Ayyapparaj
Gautham Raj Ayyapparaj

Reputation: 343

How to check if user has granted camera permission in React Native Android?

Right after the app launches, I am getting permission to use Camera from the user. But before opening camera if I check if the user has granted camera permission or not, I get the response as "false" even when it is allowed.

Here is my code:

PermissionsAndroid.check('camera').then(response => {
            if (response === true){
                //Open scanner
            }
            else if (response === false){
                Alert("Please enable camera permission in device settings.")
            }
        })

Upvotes: 5

Views: 26222

Answers (4)

vansh Kapoor
vansh Kapoor

Reputation: 41

I tried above approaches but the return type of requesting permissions is changed to boolean value.

const permissionAndroid = await PermissionsAndroid.check('android.permission.CAMERA')
if (permissionAndroid != PermissionsAndroid.RESULTS.granted) {
  ...
} // this doesnt work

instead do this

const permissionAndroid = await PermissionsAndroid.check('android.permission.CAMERA')
if( permissionAndroid ){
...
}

Upvotes: 0

vicky
vicky

Reputation: 1580

First import dependencies

import { PermissionsAndroid, Platform } from 'react-native';

Then use the following code on your event

if(Platform.OS=="android"){
      const permissionAndroid = await PermissionsAndroid.check('android.permission.CAMERA');
      if(permissionAndroid != PermissionsAndroid.RESULTS.granted){
        const reqPer = await PermissionsAndroid.request('android.permission.CAMERA');
        if(reqPer != 'granted'){
          return false;
        }
      }
    }

Upvotes: 2

Irshad Babar
Irshad Babar

Reputation: 1419

Here I am using with async,await and promise, a little bit better to understand.

import {PermissionsAndroid} from 'react-native';

export const checkReadContactsPermission = async ()=>{    

  //result would be false if not granted and true if required permission is granted.
  const result =  await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.CAMERA);
  return result;
}

Upvotes: 1

crystal_17
crystal_17

Reputation: 737

Please try as following:

PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.CAMERA).then(response => { ..

and not

PermissionsAndroid.check('camera').then(response => { ..

Upvotes: 15

Related Questions