diff options
Diffstat (limited to 'src/lib/media/manip.ts')
-rw-r--r-- | src/lib/media/manip.ts | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/src/lib/media/manip.ts b/src/lib/media/manip.ts index 4578406e0..62cbc55ac 100644 --- a/src/lib/media/manip.ts +++ b/src/lib/media/manip.ts @@ -126,6 +126,8 @@ export async function shareImageModal({uri}: {uri: string}) { safeDeleteAsync(imagePath) } +const ALBUM_NAME = 'Bluesky' + export async function saveImageToMediaLibrary({uri}: {uri: string}) { // download the file to cache // NOTE @@ -139,8 +141,34 @@ export async function saveImageToMediaLibrary({uri}: {uri: string}) { imagePath = normalizePath(await moveToPermanentPath(imagePath, '.png'), true) // save - await MediaLibrary.createAssetAsync(imagePath) - safeDeleteAsync(imagePath) + try { + if (isAndroid) { + // android triggers an annoying permission prompt if you try and move an image + // between albums. therefore, we need to either create the album with the image + // 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) + } else { + // otherwise, create album with asset (albums must always have at least one asset) + await MediaLibrary.createAlbumAsync( + ALBUM_NAME, + undefined, + undefined, + imagePath, + ) + } + } else { + await MediaLibrary.createAssetAsync(imagePath) + } + } catch (err) { + logger.error(err instanceof Error ? err : String(err), { + message: 'Failed to save image to media library', + }) + } finally { + safeDeleteAsync(imagePath) + } } export function getImageDim(path: string): Promise<Dimensions> { |