我正在为 React-Native 使用 expo。 React-Native 相机在 iPhone 中运行良好,但在 android 设备中无法打开。在 Android 设备中,当我使用 expo 打开它时它会打开,但是当我生成 apk 文件时它不会打开。我检查了它说相机没有权限的日志。请帮我解决这个问题,这是我的代码示例:
import React from 'react';
import { Text, View, TouchableOpacity } from 'react-native';
import { Camera, Permissions } from 'expo';
export default class CameraExample extends React.Component {
state = {
hasCameraPermission: null,
type: Camera.Constants.Type.back,
};
async componentWillMount() {
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>
);
}
}
}
非常感谢任何帮助。 谢谢
最佳答案
您是否在 app.json
中包含相机权限?
https://docs.expo.io/versions/latest/guides/configuration.html#permissions
它应该看起来像这样:
{
"expo": {
...
"android": {
"permissions": ["CAMERA"]
}
}
}
关于React-Native Camera 没有在 android 中打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46946137/