LynxPhotoLibrary
Saves an image or a video to the phone's photo library, without the share sheet. import { LynxPhotoLibrary, PhotoLibraryError, type PhotoLibraryFile, type PhotoLibraryPermission, type PhotoLibraryErrorCode } from '@blotch/angular-lynx';
Use it for a one-tap "Save to Photos": a photo editor's export, a ticket or a QR code, media the app downloaded. To let the person choose where a file goes (Messages, Files, AirDrop), use LynxShare instead.
Turn it on
Add permissions.savePhotos to the build target's options in angular.json, with the reason iOS shows when it asks:
What it adds:
- iOS: the photo library module, and your reason as
NSPhotoLibraryAddUsageDescription. Every app has that key, for the share sheet's Save Image. Until you give your own reason, it's "Saves the images you choose to your photo library." - Android: the photo library module. Android 10 and later need no permission. Android 7 to 9 get the storage permissions, capped at Android 9 (
maxSdkVersion="28").
Without it, the app has no photo library code, on iOS or Android. save() rejects with unavailable, and addPermission is unsupported.
See Turn on device features for every setting.
Usage
A photo from LynxImagePicker saves as it is: photos.save(picked).
API
LynxPhotoLibrary
- iOS:
PHPhotoLibrarywith add-only access, the access the share sheet's Save Image uses. The app can add photos and videos but can't see the library. iOS asks the first time, withpermissions.savePhotos's reason. - Android: MediaStore, into
Pictures/<app name>for a photo andMovies/<app name>for a video, where the gallery and Google Photos find them. Android 10 and later need no permission. - Android 7 to 9 need the storage permission:
READ_EXTERNAL_STORAGEandWRITE_EXTERNAL_STORAGE, whichpermissions.savePhotosdeclares for those versions only. The firstsave()there asks for it with Android's own message, and saves once the person allows it. - Web preview: downloads the file, as a browser has no photo library. Call
save()from the tap's handler, as forLynxShare.
Only the apps --platform builds, with ng build or ng serve, have it. Elsewhere, such as in Lynx Explorer, save() rejects with unavailable.
Under the testing library, savedPhotos() lists what was saved, and setPhotoLibraryPermission() sets what the person answers.
LynxPhotoLibrary.save()
Adds the image or the video to the library, asking the person first if the app hasn't yet. Resolves with the new item's ID:
- iOS: its
PHAssetlocal identifier - Android: its
content://URI - Web preview: the file's name
Rejects with a TypeError when file has no Blob, and with a PhotoLibraryError otherwise.
PhotoLibraryFile
addPermission
Whether the app may add to the photo library, read from the phone when the service is first injected, after each save(), and each time the app comes back to the front, since the person can change it in Settings meanwhile.
Use it to show a "Turn on in Settings" hint once the person has said no. You don't need to check it before saving: save() asks when it's undetermined.
On Android 7 to 9, a person who refused the storage prompt without ticking "Don't ask again" can still be asked. So addPermission stays undetermined, and the next save() asks again. It's denied once only Settings can change it.
PhotoLibraryError
Reading the library
permissions.photos (NSPhotoLibraryUsageDescription) is for an app whose own native code reads the whole library. Neither LynxPhotoLibrary nor LynxImagePicker, whose pickers need no permission, needs it.
It's iOS only. Android declares nothing for it, since Google Play rejects an app that declares READ_MEDIA_IMAGES without a core need for the whole library.
