Search code examples
react-nativecameraandroid-camera

Need help implementing RNCamera in a react native project


I have created a react-native project using "expo init." I want to use RNCamera, but I am getting the following error. "Possible Unhandled Promise Rejection(id:0): Error: Tried to use permissions API but the host Activity doesn't implement PermissionAwareActivity." The problem is, all of the help online seems to point to java files that don't exist in a project created with "expo init". I am trying to build a cross-platform app. Can someone please help me out as to how to do this? I'm not sure where to start since most of the help I believe is geared towards android apps.

I have tried searching the internet but have only found android-specific solutions that require editing java files that aren't in my project.

/*I don't think I have permission to upload pictures yet, but here is a list of the files in my created project. 

.expo
.git
assets
node_modules
.gitignore
.watchmanconfig
App.js
app.json
babel.config.js
package.json
yarn.lock
*/

import React, {PureComponent} from 'react';
import { View, Text, Button, StyleSheet, TouchableOpacity } from 'react-native';
import { RNCamera } from 'react-native-camera';

export default function App() {
  return (
    <View style = {{flex: 1}}>
      <RNCamera
        ref={ref => {
          this.camera = ref;
        }}
        style = {{flex: 1, width: '100%'
      }}
      >
      </RNCamera>
    </View>
  );
}

I just want to be able to access the camera. Thanks for your help!!


Solution

  • If you've created a project through Expo, it's a good idea to use the Expo module without using it. You can try this expo install expo-camera

    If you want to use the original module, or if you use the module I told you about, you must be authorized because you need storage space to store your camera and pictures.

    You can try this expo install expo-permissions

    • Camera usage rights : Permissions.CAMERA
    • video usage rights : Permissions.AUDIO_RECORDING
    • storage space rights : Permissions.CAMERA_ROLL

    Usage

    import React from 'react';
    import { Text, View, TouchableOpacity } from 'react-native';
    import * as Permissions from 'expo-permissions';
    import { Camera } from 'expo-camera';
    
    export default class CameraExample extends React.Component {
      state = {
        hasCameraPermission: null,
        type: Camera.Constants.Type.back,
      };
    
      async componentDidMount() {
        const { status } = await Permissions.askAsync(Permissions.CAMERA);
        this.setState({ hasCameraPermission: status === 'granted' });
      }
    
      render() {
        const { hasCameraPermission } = this.state;
        if (hasCameraPermission === null) {
          return <View />;
        } else if (hasCameraPermission === false) {
          return <Text>No access to camera</Text>;
        } else {
          return (
            <View style={{ flex: 1 }}>
              <Camera style={{ flex: 1 }} type={this.state.type}>
                <View
                  style={{
                    flex: 1,
                    backgroundColor: 'transparent',
                    flexDirection: 'row',
                  }}>
                  <TouchableOpacity
                    style={{
                      flex: 0.1,
                      alignSelf: 'flex-end',
                      alignItems: 'center',
                    }}
                    onPress={() => {
                      this.setState({
                        type:
                          this.state.type === Camera.Constants.Type.back
                            ? Camera.Constants.Type.front
                            : Camera.Constants.Type.back,
                      });
                    }}>
                    <Text style={{ fontSize: 18, marginBottom: 10, color: 'white' }}> Flip </Text>
                  </TouchableOpacity>
                </View>
              </Camera>
            </View>
          );
        }
      }
    }