import React, { useState } from 'react';
import { View, Button, Image, StyleSheet, TouchableOpacity } from 'react-native';
import * as ImagePicker from 'expo-image-picker';
const App = () => {
const [image, setImage] = useState(null);
const pickImage = async () => {
const { status } = await ImagePicker.requestCameraPermissionsAsync();
if (status !== 'granted') {
alert('Sorry, we need camera roll permissions to make this work!');
return;
}
const result = await ImagePicker.launchCameraAsync();
if (!result.cancelled) {
setImage(result.uri);
}
};
return (
<View style={styles.container}>
<TouchableOpacity onPress={pickImage}>
<View style={styles.button}>
<Button title="Open Camera" />
</View>
</TouchableOpacity>
{image && <Image source={{ uri: image }} style={styles.image} />}
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
button: {
backgroundColor: '#008CBA',
borderRadius: 5,
padding: 10,
},
image: {
width: 200,
height: 200,
marginTop: 20,
},
});
export default App;
This code uses the expo-image-picker library to request camera permissions and launch the camera when the button is pressed. The selected image is displayed below the button. Note that you will need to install the expo-image-picker library before running this code. You can do this by running npm install expo-image-picker in your project directory.