> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# 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`](/guide/share.md) instead.

## Turn it on

Add `permissions.savePhotos` to the `build` target's options in angular.json, with the [reason](/guide/native-apps/permissions.md) iOS shows when it asks:

```json title="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](/guide/native-apps/device-features.md) for every setting.

## Usage

```typescript
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`](/guide/image-picker.md) saves as it is: `photos.save(picked)`.

## API

### `LynxPhotoLibrary`

```typescript
@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()`](/api/testing-library/fakes/media.md#savedphotos) lists what was saved, and `setPhotoLibraryPermission()` sets what the person answers.

### `LynxPhotoLibrary.save()`

```typescript
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`](#photolibraryerror) otherwise.

### `PhotoLibraryFile`

```typescript
type PhotoLibraryFile = {
  readonly blob: Blob;
  readonly name?: string;
};
```

| Field  | Description                                                                                                                                                                       |
| ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `blob` | The image or the video. Its `type` says which (`image/jpeg`, `video/mp4`); when it has none, the name's extension does                                                            |
| `name` | The 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`

```typescript
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`.

| Value          | Meaning                                                                                                                                                                                                            |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `granted`      | The app may add. Always so on Android 10 and later, and in the web preview                                                                                                                                         |
| `limited`      | iOS's limited library access. Adding still works                                                                                                                                                                   |
| `denied`       | The person said no, or a restriction such as Screen Time blocks it. Only Settings can change it: [`openAppSettings()`](/api/angular-lynx/sharing/linking.md#lynxlinkingopenappsettings) opens the app's page there |
| `undetermined` | The app hasn't asked yet; the first `save()` asks. Also what it holds until the phone first answers                                                                                                                |
| `unsupported`  | The 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`

```typescript
class PhotoLibraryError extends Error {
  readonly code: PhotoLibraryErrorCode;
}

type PhotoLibraryErrorCode =
  'permission-denied' | 'unsupported-type' | 'unavailable' | 'failed';
```

| `code`              | When                                                                                                                         |
| ------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| `permission-denied` | The person didn't let the app add to the library, now or before. If `addPermission` is `denied`, only Settings can change it |
| `unsupported-type`  | The 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         |
| `unavailable`       | The host has no photo library: no `permissions.savePhotos`, or not a `--platform` app                                        |
| `failed`            | The 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.
