我在Expo -image-picker 13.3.1的Expo SDK 46中遇到了这个问题。
甚至当我请求相机权限时,这些权限被授予,当我使用ImagePicker.launchCameraAsync()时,它会向我抛出一个错误"用户拒绝权限"。const result = await ImagePicker.launchCameraAsync({ mediaTypes: ImagePicker.MediaTypeOptions.Images, allowsEditing: false, base64: false, });
我升级和降级了库,但我无法解决这个问题。
检查我的样本,它为我工作。
import { Camera, CameraType } from 'expo-camera';
import { useState } from 'react';
import { Button, StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import * as ImagePicker from 'expo-image-picker';
const openCamera = async () => {
const result = await ImagePicker.launchCameraAsync({
mediaTypes: ImagePicker.MediaTypeOptions.Images,
allowsEditing: false,
base64: false,
aspect: [4, 3],
quality: 1,
});
console.log(result);
};
export default function App() {
const [permission, requestPermission] = Camera.useCameraPermissions();
if (!permission) {
// Camera permissions are still loading
return <View />;
}
if (!permission.granted) {
// Camera permissions are not granted yet
return (
<View style={styles.container}>
<Text style={{ textAlign: 'center' }}>
We need your permission to show the camera
</Text>
<Button onPress={requestPermission} title="grant permission" />
</View>
);
}
return (
<View style={styles.container}>
<View style={styles.buttonContainer}>
<TouchableOpacity style={styles.button} onPress={openCamera}>
<Text style={styles.text}>Open Camera</Text>
</TouchableOpacity>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
},
buttonContainer: {
flex: 1,
flexDirection: 'row',
backgroundColor: 'transparent',
margin: 64,
},
button: {
flex: 1,
alignSelf: 'flex-end',
alignItems: 'center',
},
text: {
fontSize: 24,
fontWeight: 'bold',
color: 'black',
},
});