From e3f596740701de869a5831ca2132441bc3768786 Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Tue, 28 Oct 2025 18:58:41 +0200 Subject: [PATCH] Android - add fallbacks when saving image to gallery (#9293) * add fallback paths when saving image to gallery * `debug` -> `info` --- src/lib/media/manip.ts | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/lib/media/manip.ts b/src/lib/media/manip.ts index f8f8d0703d..bf6843f542 100644 --- a/src/lib/media/manip.ts +++ b/src/lib/media/manip.ts @@ -114,8 +114,39 @@ export async function saveImageToMediaLibrary({uri}: {uri: string}) { // as the starting image, or put it directly into the album const album = await MediaLibrary.getAlbumAsync(ALBUM_NAME) if (album) { - // if album exists, put the image straight in there - await MediaLibrary.createAssetAsync(imagePath, album) + // try and migrate if needed + 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 { // otherwise, create album with asset (albums must always have at least one asset) await MediaLibrary.createAlbumAsync( @@ -132,6 +163,7 @@ export async function saveImageToMediaLibrary({uri}: {uri: string}) { logger.error(err instanceof Error ? err : String(err), { message: 'Failed to save image to media library', }) + throw err } finally { safeDeleteAsync(imagePath) }