react-native-vision-camera device is null

2.5k Views Asked by At

I am trying to run a basic example of react-native-vision-camera and getting the error device is null. I checked the app permissions through android phone and camera permissions are granted. Here is my current code:


import React,{useEffect, useState} from 'react';
import { Text, View, Linking} from 'react-native';
import {useCameraDevices, Camera} from 'react-native-vision-camera';


const App = () => {

  const devices = useCameraDevices();
  const device = devices.back;

React.useEffect( () => {
  requestCameraPermission();
}, [])


const requestCameraPermission = React.useCallback( async () => {

const permission = await Camera.requestCameraPermission();

if (permission == 'denied')
{
console.log("Permission not granted");  
await Linking.openSettings();
}

  }, [])


  function renderCamera()
  {
    if(device == null)
    {
      console.log("device is null");
      
      return (<View><Text>Camera not working</Text></View>)
    }
    else 
    {
      <View>
        <Camera
       
        device={device}
        isActive={true}
        enableZoomGesture
        />
      </View>
    }
  }
  
    return(
  <View>
    
    {renderCamera()}
  </View>
    )
  }  
    
 export default App;

1

There are 1 best solutions below

0
On

Instead of using:

const devices = useCameraDevices();
const device = devices.back;

use this:

const device = useCameraDevices('back');

Hope it will help.