After taking a photo using the system camera, I move it into an album using MediaLibrary. Afterwards I want to display the photo in the application but the new file is not contained inside the asset array of getAssetsAsync.
The new file isn’t appearing because you are not waiting for the createAlbumAsync or addAssetsToAlbumAsync methods to finish. You need to use await with them and handle their responses accordingly.
Here are the changes needed:
const cachedAsset = await MediaLibrary.createAssetAsync(result.uri);
const album = await MediaLibrary.getAlbumAsync(constants.gallery_name);
if (album === null) {
let albumCreated = await MediaLibrary.createAlbumAsync(
constants.gallery_name,
cachedAsset,
false
);
if (albumCreated) {
this.getAssetFromAlbum(albumCreated);
} else {
//handle album creation error
}
} else {
let assetAdded = await MediaLibrary.addAssetsToAlbumAsync(
[cachedAsset],
album,
false
);
if (assetAdded === true) {
this.getAssetFromAlbum(album);
} else {
// handle asset add error
}
}
}
};
getAssetFromAlbum = async album => {
const assetResult = await MediaLibrary.getAssetsAsync({
first: 1,
album: album,
sortBy: MediaLibrary.SortBy.creationTime,
});
const asset = await assetResult.assets[0];
};
thank you but unfortunately it still behaves the same with your updated code. getAssetFromAlbum doesn’t return the newly taken photo but one asset before that.
Alright, I just tested it on my Galaxy S8 and I’m getting the same behavior you described so it looks like it’s an Android bug. Can you create a Github issue on our expo repo detailing the issue and add your Snack there? Issues · expo/expo · GitHub