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

# LynxImagePicker

Lets someone pick a photo with the phone's photo picker, or take one with its camera, and hands it back as a `Blob`. `import { LynxImagePicker, ImagePickerError, type ImagePickerOptions, type ImagePickerSource, type ImagePickerErrorCode, type PickedImage } from '@blotch/angular-lynx';`

[Pick a Photo](/guide/image-picker.md) shows it in use.

## Turn it on

Picking from the library needs no setting: every app `ng build --platform` makes has it. Only the camera needs one.

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

```json title="angular.json"
"options": {
  "permissions": { "camera": "Take a photo of the receipt" }
}
```

What it adds:

- **iOS:** `NSCameraUsageDescription`, the reason iOS's camera prompt shows, and the picker's camera code.
- **Android:** the `CAMERA` permission, which Android asks for before the picker opens the camera app, and the picker's camera code. The camera is declared not required, so Google Play still offers the app to devices without one.

It also turns on [`LynxBarcodeScanner`](/api/angular-lynx/hardware/barcode-scanner.md).

Without it, the camera code is left out of both apps, and `pick({ source: 'camera' })` rejects with `unavailable`. App Store Connect flags an app whose code uses the camera without saying why. The library picker is still there.

See [Turn on device features](/guide/native-apps/device-features.md) for every setting.

## Usage

```typescript
const picker = inject(LynxImagePicker);

const picked = await picker.pick({ maxDimension: 1024, quality: 0.85 });
if (picked) {
  await uploadBytes(storageRef, picked.blob, { contentType: picked.type });
}
```

## API

### `LynxImagePicker`

```typescript
@Injectable({ providedIn: 'root' })
class LynxImagePicker {}
```

| Platform                                          | Library                                                                             | Camera                                                         |
| ------------------------------------------------- | ----------------------------------------------------------------------------------- | -------------------------------------------------------------- |
| iOS                                               | `PHPickerViewController`                                                            | `UIImagePickerController`                                      |
| Android                                           | The Photo Picker                                                                    | The phone's camera app, after asking for the camera permission |
| [Web preview](/guide/glossary.md#web-preview)     | An `<input type="file" accept="image/*">`                                           | The same input, with `capture="environment"`                   |
| [Lynx Explorer](/guide/glossary.md#lynx-explorer) | Rejects with `unavailable`                                                          | Rejects with `unavailable`                                     |
| Unit tests with the testing library               | What [`setPickedImage()`](/api/testing-library/fakes/media.md#setpickedimage) gives | What `setTakenPhoto()` gives                                   |

Only the apps `--platform` builds, with `ng build` or `ng serve`, have the native module behind it. The camera is only in apps whose angular.json sets `permissions.camera`.

Under the testing library, `pick()` rejects with `unavailable` until a spec gives it a photo.

### `LynxImagePicker.pick()`

```typescript
pick(options?: ImagePickerOptions): Promise<PickedImage | null>;
```

Opens the picker, or the camera with `source: 'camera'`. Resolves with the photo picked or taken, or with `null` when the person closes it without choosing. Rejects with an [`ImagePickerError`](#imagepickererror), or with a `RangeError` for an option out of range.

One picker can be open at a time. Picking again while one is open:

- **On iOS and Android,** rejects with `failed`.
- **In the web preview,** resolves the open pick with `null`.

### `LynxImagePicker.recover()`

```typescript
recover(): Promise<PickedImage | null>;
```

Resolves with the photo the person picked or took while Android restarted the app, which no `pick()` was left to resolve with, or with `null` when there's none. Rejects with an [`ImagePickerError`](#imagepickererror) with `failed` when that photo couldn't be read.

Android can end the app's screen while the picker or the camera is in front, when it needs the memory or with "Don't keep activities" on, and start it again when the person has chosen. The app starts again with it, so the `pick()` that opened the picker has gone. Call `recover()` as the app starts:

```typescript
const lost = await picker.recover();
if (lost) this.attach(lost);
```

- **It resolves with the photo once,** prepared with the `maxDimension` and `quality` the lost `pick()` asked for.
- **It waits** while Android still has to hand the restarted app the result, until the picker or the camera closes.
- **It reads the photo only when it's called,** so an app that never calls it doesn't read it.
- **A photo nobody asks for** isn't handed over after a minute, and is dropped when another picker or camera opens, even if the person cancels it, or when the person closes the app or the app ends.

| Platform                                                                                                          | `recover()`                                                    |
| ----------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------- |
| Android                                                                                                           | The photo picked or taken while Android restarted the app      |
| iOS, [web preview](/guide/glossary.md#web-preview), [Lynx Explorer](/guide/glossary.md#lynx-explorer), unit tests | `null`: iOS doesn't end an app's screen while a picker is open |

### `ImagePickerOptions`

```typescript
type ImagePickerOptions = {
  readonly source?: ImagePickerSource;
  readonly maxDimension?: number;
  readonly quality?: number;
};

type ImagePickerSource = 'library' | 'camera';
```

| Option         | Description                                                                                                                 |
| -------------- | --------------------------------------------------------------------------------------------------------------------------- |
| `source`       | `library` (the default) to pick a photo the person has, or `camera` to take a new one, which needs [a setting](#turn-it-on) |
| `maxDimension` | Scales the photo down, if it's larger, so its longest side has at most this many pixels, at least 1. It's never scaled up   |
| `quality`      | The JPEG quality of the re-encoded photo, from 0 to 1. Defaults to 0.9                                                      |

**Either option re-encodes the photo as a JPEG:**

- upright, whichever way the camera stored it
- without the original's metadata
- with a white background where it was transparent

**Without them,** a JPEG, PNG, GIF or WebP comes back as it was picked. Any other format, such as HEIC, is re-encoded as a JPEG at full size.

A photo from the camera is always re-encoded as a JPEG.

### `PickedImage`

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

| Property | Description                                                                |
| -------- | -------------------------------------------------------------------------- |
| `blob`   | The photo's bytes                                                          |
| `name`   | A file name for it, such as `IMG_0042.jpg`, whose extension matches `type` |
| `type`   | Its MIME type, such as `image/jpeg`: the same as `blob.type`               |

- **On Android,** `name` is the photo's number in the library, such as `1000000020.jpg`: the Photo Picker doesn't share the file's own name.
- **A photo from the camera** is named for when it was taken, such as `IMG_20261129_142305.jpg`.

### `ImagePickerError`

```typescript
class ImagePickerError extends Error {
  readonly code: ImagePickerErrorCode;
}

type ImagePickerErrorCode = 'unavailable' | 'denied' | 'failed';
```

| `code`        | When                                                                                                                                             |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| `unavailable` | The host has no photo picker. For the camera, also an app without `permissions.camera`, and a device without a camera, such as the iOS Simulator |
| `denied`      | The person didn't let the app use the camera                                                                                                     |
| `failed`      | The picker couldn't open, one was already open, or the picked photo couldn't be read or decoded                                                  |
