This app asks for camera permission, shows the camera preview, and has a button to take a picture. When you tap the button, it takes a photo and shows the file path below.
import React, { useState, useEffect } from 'react';
import { Text, View, TouchableOpacity } from 'react-native';
import { Camera } from 'expo-camera';
export default function App() {
const [hasPermission, setHasPermission] = useState(null);
const [cameraRef, setCameraRef] = useState(null);
const [photoUri, setPhotoUri] = useState(null);
useEffect(() => {
(async () => {
const { status } = await Camera.requestCameraPermissionsAsync();
setHasPermission(status === 'granted');
})();
}, []);
const takePicture = async () => {
if (cameraRef) {
const photo = await cameraRef.takePictureAsync();
setPhotoUri(photo.uri);
}
};
if (hasPermission === null) {
return <View />;
}
if (hasPermission === false) {
return <Text>No access to camera</Text>;
}
return (
<View style={{ flex: 1 }}>
<Camera style={{ flex: 4 }} ref={ref => setCameraRef(ref)} />
<TouchableOpacity
onPress={takePicture}
style={{ flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#2196F3' }}
accessibilityLabel="Take picture button"
>
<Text style={{ color: 'white', fontSize: 18 }}>Take Picture</Text>
</TouchableOpacity>
{photoUri && <Text style={{ padding: 10 }}>Photo saved at: {photoUri}</Text>}
</View>
);
}