【问题标题】:Need help implementing RNCamera in a react native project需要帮助在 React Native 项目中实现 RNCamera
【发布时间】:2019-07-01 23:32:28
【问题描述】:

我使用“expo init”创建了一个 react-native 项目。我想使用 RNCamera,但出现以下错误。 “可能的未处理承诺拒绝(id:0):错误:尝试使用权限 API,但主机 Activity 未实现 PermissionAwareActivity。”问题是,所有在线帮助似乎都指向使用“expo init”创建的项目中不存在的 java 文件。我正在尝试构建一个跨平台的应用程序。有人可以帮我解决这个问题吗?我不知道从哪里开始,因为我认为大部分帮助都是针对 Android 应用程序的。

我尝试在互联网上搜索,但只找到了需要编辑不在我的项目中的 java 文件的特定于 android 的解决方案。

/*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>
  );
}

我只想能够访问相机。谢谢你的帮助!!

【问题讨论】:

    标签: react-native camera android-camera


    【解决方案1】:

    如果您通过 Expo 创建了一个项目,最好使用 Expo 模块而不使用它。你可以试试这个expo install expo-camera

    如果你想使用原装模组,或者如果你使用我告诉你的模组,你必须获得授权,因为你需要存储空间来存储你的相机和照片。

    你可以试试这个expo install expo-permissions

    • 相机使用权限:Permissions.CAMERA
    • 视频使用权:Permissions.AUDIO_RECORDING
    • 存储空间权限:Permissions.CAMERA_ROLL

    用法

    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>
          );
        }
      }
    }
    

    【讨论】:

    • 非常感谢!!我离开了我的房子,但我回来后会继续努力。再次感谢!!
    • @Matt123 如果我的回答对您有帮助,您可以选择一个吗?
    • 再次感谢您的帮助。我想知道,这个解决方案会跨平台吗?如果没有,我该如何让它跨平台?
    • @Matt123 这个功能部分是交叉可能的地方。并非所有功能都在 React-native 模块中交叉。最好查看这些领域的文档。有些区域需要添加设置或需要额外的权限,而不是完全不同的功能。对了,你查了我的答案,为什么现在没有我的答案?
    猜你喜欢
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多