Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛[android] Camera.requestCameraPermission() does not resolve after giving camera permissions. #1423

Closed
3 of 4 tasks
jlemein opened this issue Jan 20, 2023 · 4 comments · Fixed by #1466
Closed
3 of 4 tasks
Labels
🐛 bug Something isn't working

Comments

@jlemein
Copy link

jlemein commented Jan 20, 2023

What were you trying to do?

The following are my reproduction steps for Android (phone and emulator).

  1. I request for camera permission by calling await Camera.requestCameraPermission();.
  2. A popup dialog is shown asking me to decide on which permission to give. I answer "only this time".
  3. The popup dialog disappears but I have no access to the camera.
    • Also if you look at the logs, it does not print "Permission given: ...".
  4. If I restart the app, and use the camera again, I have the permissions already and the call resolves, showing me the camera.

Reproduceable Code

import React, { useEffect, useState } from 'react';
import { ActivityIndicator, StyleSheet, Text, View } from "react-native";
import { Camera, useCameraDevices, CameraPermissionStatus } from 'react-native-vision-camera';

const styles = StyleSheet.create({
    container: {
        flex: 1,
        alignItems: 'center',
        justifyContent: 'center'
    }
})

export default function CameraScreen(): JSX.Element {
    const [cameraPermission, setCameraPermission] = useState<CameraPermissionStatus>("not-determined");
    const [error, setError] = useState<string>("")

    useEffect(() => {
        const requestPermissions = async () => {
            console.log("Requesting camera permission");
            const permission = await Camera.requestCameraPermission();
            console.log("Permission given: ", permission);

            if (permission !== "authorized") {
                setError("Not allowed to access camera");
            }

            setCameraPermission(permission);
        }
        
        requestPermissions();
    });

    const devices = useCameraDevices('wide-angle-camera');
    const device = devices.back;

    const renderCamera = () => {
        if (error) {
            return (<Text>Error: {error}</Text>);
        }

        if (cameraPermission === "not-determined") {
            return <ActivityIndicator />
        }    

        if (device == null) {
            return (<Text>No camera device could be found on your device.</Text>)
        }

        return (
            <Camera
                style={StyleSheet.absoluteFill}
                device={device}
                isActive={true}
            />
        )
    }

    return (
        <View style={styles.container}>
            {renderCamera()}
        </View>
    )
};

What happened instead?

It seems the call await Camera.requestCameraPermission() does not resolve when it requests for camera permissions. It hangs and the camera never appears.
I would expect the call to resolve so that I can use the camera.

Relevant log output

No response

Device

Android v11 (Realme 6 Pro)

VisionCamera Version

2.15.2

Additional information

@jlemein jlemein added the 🐛 bug Something isn't working label Jan 20, 2023
@ssangram111
Copy link

ssangram111 commented Jan 24, 2023

i am using this its working fine

const requestCameraPermission = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA,
{
title: 'Cool Photo App Camera Permission',
message:
'Cool Photo App needs access to your camera '
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('You can use the camera');
} else {
console.log('Camera permission denied');
}
} catch (err) {
console.warn(err);
}
};

@jlemein
Copy link
Author

jlemein commented Feb 3, 2023

I managed to get it working too by using PermissionsAndroid as a workaround.
It is still a bug in react-native-vision-camera though.

@alexhernandez
Copy link

I was able to get it to work by doing the following:

Android Manifest:

  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.CAMERA" />
  <uses-permission android:name="android.permission.RECORD_AUDIO" />

  <uses-feature android:name="android.hardware.camera.any" android:required="false" />
  <uses-feature android:name="android.hardware.camera" android:required="false" />

Methods:

async function checkPermissions() {
  let cameraPermission: CameraPermissionStatus = 'not-determined';
  let microphonePermission: CameraPermissionStatus = 'not-determined';

  try {
    cameraPermission = await RNCamera.getCameraPermissionStatus();
    microphonePermission = await RNCamera.getMicrophonePermissionStatus();
  } catch (e) {
    console.log(e, 'checkPermissions');
  }

  return {
    camera: cameraPermission,
    microphone: microphonePermission,
  };
}

async function requestPermissions() {
  let cameraPermission: CameraPermissionStatus = 'denied';
  let microphonePermission: CameraPermissionStatus = 'denied';

  try {
    cameraPermission = await RNCamera.requestCameraPermission();
    microphonePermission = await RNCamera.requestMicrophonePermission();
  } catch (e) {
    console.log(e, 'requestPermissions');
  }

  return {
    camera: cameraPermission,
    microphone: microphonePermission,
  };
}

@myselfuser1
Copy link

Try this https://www.youtube.com/playlist?list=PLQhQEGkwKZUrempLnmxjt7ZCZJu1W3p2i

@mrousavy
Copy link
Owner

Hey! I've rewritten the entire Android codebase of VisionCamera from CameraX to Camera2 in the efforts of ✨ VisionCamera V3.

I just now completed the Camera2 rewrite and I believe the core structure is running, but there might be some edge cases to iron out. Can you try and test the PR #1674 for me to see if you can still reproduce this issue here?

Here's an instruction on how you can test that: #1674 (comment)

If the issue cannot be reproduced with that version/PR anymore, then hoorayy, I fixed it! 🎉
Otherwise please let me know and I'll keep this issue open to keep track of it.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants