For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /api/angular-lynx/media/photo-library.md.

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:

angular.json
"options": {
  "permissions": { "savePhotos": "Save your edits to Photos" }
}

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

const photos = inject(LynxPhotoLibrary);
// Shows an "Open Settings" button (LynxLinking.openAppSettings()). Computed
// from addPermission, so it goes once the person allows the library there.
const askForSettings = computed(() => photos.addPermission() === 'denied');

canvas.toBlob(
  async (blob) => {
    if (!blob) return;
    try {
      await photos.save({ blob, name: 'edit.jpg' });
      this.saved.set(true);
    } catch (error) {
      if (
        error instanceof PhotoLibraryError &&
        error.code === 'permission-denied'
      ) {
        // A "denied" shows askForSettings' button. On Android 7 to 9 a refusal
        // without "Don't ask again" stays "undetermined": the next save asks.
        return;
      }
      throw error;
    }
  },
  'image/jpeg',
  0.9,
);

A photo from LynxImagePicker saves as it is: photos.save(picked).

API

LynxPhotoLibrary

@Injectable({ providedIn: 'root' })
class LynxPhotoLibrary {
  readonly addPermission: Signal<PhotoLibraryPermission>;
  save(file: PhotoLibraryFile): Promise<string>;
}
  • iOS: PHPhotoLibrary with 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, with permissions.savePhotos's reason.
  • Android: MediaStore, into Pictures/<app name> for a photo and Movies/<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_STORAGE and WRITE_EXTERNAL_STORAGE, which permissions.savePhotos declares for those versions only. The first save() 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 for LynxShare.

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()

save(file: PhotoLibraryFile): Promise<string>;

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 PHAsset local 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

type PhotoLibraryFile = {
  readonly blob: Blob;
  readonly name?: string;
};
FieldDescription
blobThe image or the video. Its type says which (image/jpeg, video/mp4); when it has none, the name's extension does
nameThe file name, which Android's gallery and the web preview's download show. A photo without one is saved as Photo-<time>.<extension>, and a video as Video-<time>.<extension>

addPermission

readonly addPermission: Signal<PhotoLibraryPermission>;

type PhotoLibraryPermission = 'granted' | 'limited' | 'denied' | 'undetermined' | 'unsupported';

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.

ValueMeaning
grantedThe app may add. Always so on Android 10 and later, and in the web preview
limitediOS's limited library access. Adding still works
deniedThe person said no, or a restriction such as Screen Time blocks it. Only Settings can change it: openAppSettings() opens the app's page there
undeterminedThe app hasn't asked yet; the first save() asks. Also what it holds until the phone first answers
unsupportedThe host has no photo library module: no permissions.savePhotos, Lynx Explorer

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

class PhotoLibraryError extends Error {
  readonly code: PhotoLibraryErrorCode;
}

type PhotoLibraryErrorCode =
  'permission-denied' | 'unsupported-type' | 'unavailable' | 'failed';
codeWhen
permission-deniedThe person didn't let the app add to the library, now or before. If addPermission is denied, only Settings can change it
unsupported-typeThe file isn't an image or a video, by its Blob's type or its name's extension, or the library can't read its format
unavailableThe host has no photo library: no permissions.savePhotos, or not a --platform app
failedThe file couldn't be written to the library, such as when the phone is out of space

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.