im trying to access media gallery from a phone is display it in my expo app using the expo media gallery, everything seem to work fine but the image are not displaying.
it seems to be an issue with the uri path which is prefix with ph:// instead of assets://* for iOS and file://* for Android as stated in the documentation
import { useState, useEffect } from 'react';
import { Button, Text, SafeAreaView, ScrollView, StyleSheet, Image, View, Platform } from 'react-native';
import * as MediaLibrary from 'expo-media-library';
function AlbumEntry({ album }) {
const [assets, setAssets] = useState([]);
useEffect(() => {
async function getAlbumAssets() {
const albumAssets = await MediaLibrary.getAssetsAsync({ album });
setAssets(albumAssets.assets);
}
getAlbumAssets();
}, [album]);
return (
<View key={album.id} style={styles.albumContainer}>
<Text>
{album.title} - {album.assetCount ?? 'no'} assets
</Text>
<View style={styles.albumAssetsContainer}>
{assets && assets.map((asset) =>{
console.log(asset.uri)
return (
<View key={asset.id}>
<Image source={{ uri: asset.uri }} style={{ width: 50, height: 50 }} />
</View>
)
})}
</View>
</View>
);
}
export default function MediaGallery() {
const [albums, setAlbums] = useState(null);
const [permissionResponse, requestPermission] = MediaLibrary.usePermissions();
async function getAlbums() {
if (permissionResponse.status !== 'granted') {
await requestPermission();
}
const fetchedAlbums = await MediaLibrary.getAlbumsAsync({
includeSmartAlbums: true,
});
setAlbums(fetchedAlbums);
}
return (
<SafeAreaView style={styles.container}>
<Button onPress={getAlbums} title="Get albums" />
<ScrollView>
{albums && albums.map((album) => <AlbumEntry key={album.id} album={album} />)}
</ScrollView>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
gap: 8,
justifyContent: 'center',
...Platform.select({
android: {
paddingTop: 40,
},
}),
},
albumContainer: {
paddingHorizontal: 20,
marginBottom: 12,
gap: 4,
},
albumAssetsContainer: {
flexDirection: 'row',
flexWrap: 'wrap',
},
});
so i have been able to make the image display by using expo-image package.