Android - add fallbacks when saving image to gallery (#9293)

* add fallback paths when saving image to gallery

* `debug` -> `info`
This commit is contained in:
Samuel Newman
2025-10-28 18:58:41 +02:00
parent f9a502f393
commit f1803068ae
+34 -2
View File
@@ -114,8 +114,39 @@ export async function saveImageToMediaLibrary({uri}: {uri: string}) {
// as the starting image, or put it directly into the album // as the starting image, or put it directly into the album
const album = await MediaLibrary.getAlbumAsync(ALBUM_NAME) const album = await MediaLibrary.getAlbumAsync(ALBUM_NAME)
if (album) { if (album) {
// if album exists, put the image straight in there // try and migrate if needed
await MediaLibrary.createAssetAsync(imagePath, album) try {
if (await MediaLibrary.albumNeedsMigrationAsync(album)) {
await MediaLibrary.migrateAlbumIfNeededAsync(album)
}
} catch (err) {
logger.info('Attempted and failed to migrate album', {
safeMessage: err,
})
}
try {
// if album exists, put the image straight in there
await MediaLibrary.createAssetAsync(imagePath, album)
} catch (err) {
logger.info('Failed to create asset', {safeMessage: err})
// however, it's possible that we don't have write permission to the album
// try making a new one!
try {
await MediaLibrary.createAlbumAsync(
ALBUM_NAME,
undefined,
undefined,
imagePath,
)
} catch (err2) {
logger.info('Failed to create asset in a fresh album', {
safeMessage: err2,
})
// ... and if all else fails, just put it in DCIM
await MediaLibrary.createAssetAsync(imagePath)
}
}
} else { } else {
// otherwise, create album with asset (albums must always have at least one asset) // otherwise, create album with asset (albums must always have at least one asset)
await MediaLibrary.createAlbumAsync( await MediaLibrary.createAlbumAsync(
@@ -132,6 +163,7 @@ export async function saveImageToMediaLibrary({uri}: {uri: string}) {
logger.error(err instanceof Error ? err : String(err), { logger.error(err instanceof Error ? err : String(err), {
message: 'Failed to save image to media library', message: 'Failed to save image to media library',
}) })
throw err
} finally { } finally {
safeDeleteAsync(imagePath) safeDeleteAsync(imagePath)
} }