2

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!!

Matt123
  • 393
  • 6
  • 18

1 Answers1

1

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>
      );
    }
  }
}
hong developer
  • 13,291
  • 4
  • 38
  • 68
  • Thanks so much!! I left my house but will work on this when I get back. Thanks again!! – Matt123 Jul 02 '19 at 01:00
  • @Matt123 If my answer was helpful, could you choose one? – hong developer Jul 02 '19 at 01:01
  • Thanks again for your help. I was wondering, will this solution be cross-platform? If not, how do I make it cross-platform? – Matt123 Jul 03 '19 at 16:06
  • @Matt123 This functional part is where the cross is possible. Not all functions cross in React-native Module. It is good to look at documents in such areas. There are areas where setup is added or where authority is required additionally, not completely different functions. By the way, you checked my answer, but why did you not my answer it now? – hong developer Jul 04 '19 at 00:46