Expo gives you access to the camera, location, notifications, and more through a set of managed APIs. Today you will request permissions, access the camera roll, and read the device location.
npx expo install expo-camera expo-location expo-image-picker// CameraExample.js
import { useState } from 'react';
import { Button, Image, View } from 'react-native';
import * as ImagePicker from 'expo-image-picker';
export default function CameraExample() {
const [image, setImage] = useState(null);
async function pickImage() {
const { status } = await ImagePicker.requestMediaLibraryPermissionsAsync();
if (status !== 'granted') return alert('Permission denied');
const result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.Images,
allowsEditing: true,
aspect: [4, 3],
quality: 0.7,
});
if (!result.canceled) setImage(result.assets[0].uri);
}
return (
<View style={{ flex:1, alignItems:'center', padding:20 }}>
<Button title="Pick an image" onPress={pickImage} />
{image && <Image source={{ uri: image }} style={{ width:300, height:225, marginTop:16 }} />}
</View>
);
}// LocationExample.js
import { useEffect, useState } from 'react';
import { Text, View } from 'react-native';
import * as Location from 'expo-location';
export default function LocationExample() {
const [loc, setLoc] = useState(null);
useEffect(() => {
(async () => {
const { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') return;
const pos = await Location.getCurrentPositionAsync({});
setLoc(pos.coords);
})();
}, []);
return (
<View style={{ padding:20 }}>
{loc ? <Text>{loc.latitude.toFixed(4)}, {loc.longitude.toFixed(4)}</Text>
: <Text>Getting location...</Text>}
</View>
);
}