> For the complete documentation index, see [llms.txt](https://developers-apps-in-toss.toss.im/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers-apps-in-toss.toss.im/documentation/api-and-sdk-zh/sdk/domains-api/she-bei/device.getphotos.md).

# Device.getPhotos

> 函数式 API `fetchAlbumPhotos`也能用它实现相同功能。

### 功能说明

从用户相册中读取照片列表。可以设置最大数量和分辨率，可用于图库预览、图片选择等。

如果没有相册读取权限 `FetchAlbumPhotosPermissionError`会发生。 `Device.getPhotos.getPermission()`来确认权限状态， `Device.getPhotos.openPermissionDialog()`并可再次请求权限。

### 类型

```ts
Device.getPhotos(options?: FetchAlbumPhotosOptions): Promise<ImageResponse[]>;
```

**参数**

```ts
type FetchAlbumPhotosOptions = {
  /** 要获取的照片最大数量。默认值为 10。 */
  maxCount?: number;
  /** 图片的最大宽度（px）。默认值为 1024。 */
  maxWidth?: number;
  /** 是否以 base64 返回图片。默认值为 false。 */
  base64?: boolean;
};
```

**响应**

```ts
type ImageResponse = {
  id: string;
  dataUri: string;
};
```

### 错误

错误会以类的形式抛出 — `error instanceof FetchAlbumPhotosPermissionError`通过它识别。该类可在 `@apps-in-toss/web-framework`中导入。

| 错误类                               | 说明                                                            |
| --------------------------------- | ------------------------------------------------------------- |
| `FetchAlbumPhotosPermissionError` | 表示相册读取权限被拒绝。 `Device.getPhotos.openPermissionDialog()`可以重新请求。 |

### 示例代码

```js
import {
  Device,
  FetchAlbumPhotosPermissionError,
} from "@apps-in-toss/web-framework";

const base64 = true;

async function renderAlbumPhotos() {
  try {
    const photos = await Device.getPhotos({
      base64,
      maxCount: 10,
      maxWidth: 360,
    });

    const container = document.getElementById("photo-list");
    for (const photo of photos) {
      const img = document.createElement("img");
      img.src = base64
        ? "data:image/jpeg;base64," + photo.dataUri
        : photo.dataUri;
      container.appendChild(img);
    }
  } catch (error) {
    if (error instanceof FetchAlbumPhotosPermissionError) {
      console.warn("没有相册读取权限。");
      return;
    }
    console.error(error);
  }
}

document
  .getElementById("load-photos")
  .addEventListener("click", renderAlbumPhotos);
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://developers-apps-in-toss.toss.im/documentation/api-and-sdk-zh/sdk/domains-api/she-bei/device.getphotos.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
