> 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.getalbumitems.md).

# Device.getAlbumItems

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

### 功能说明

从用户相册中选择并获取照片·视频。若取消选择，则返回空数组 `[]`并返回， `types`如果不指定，则只会选择照片。

Toss App Android/iOS `5.261.0` 可在以上版本中使用。调用前 `Device.getAlbumItems.isSupported()`可以通过它确认是否支持。在不支持的版本中调用时 `UNSUPPORTED_APP_VERSION` 会发生错误。

### 类型

```ts
Device.getAlbumItems(options?: FetchAlbumItemsOptions): Promise<AlbumItemResponse[]>;
```

**参数**

```ts
type AlbumItemType = "PHOTO" | "VIDEO";

type FetchAlbumItemsOptions = {
  /** 要获取的媒体类型列表。若不指定，则只获取照片。 */
  types?: AlbumItemType[];
  /** 要获取的项目最大数量。默认值为 10。 */
  maxCount?: number;
  /** 图片的最大宽度（px）。默认值为 1024。 */
  maxWidth?: number;
  /** 是否将图片 dataUri 以 Base64 返回。默认值为 false。 */
  base64?: boolean;
};
```

**响应**

```ts
type AlbumItemResponse = {
  id: string;
  dataUri: string;
  type: "PHOTO" | "VIDEO";
};
```

### 错误

错误代码是 catch 到的错误的 `error.code` 值。

| 代码                        | 说明                                                                                                                  |
| ------------------------- | ------------------------------------------------------------------------------------------------------------------- |
| `NOT_ALLOWED`             | 不允许访问相册的情况。                                                                                                         |
| `INVALID_REQUEST`         | 请求参数不正确的情况。                                                                                                         |
| `INVALID_DATA`            | 媒体数据无效的情况。                                                                                                          |
| `UNSUPPORTED_APP_VERSION` | 如果当前运行的 Toss App 版本不支持此功能，则调用后会立即发生。 `Device.getAlbumItems.isSupported()`请先通过它确认，发生时 `error.message`请向用户显示（更新提示文案）。 |

### 示例代码

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

async function pickAlbumItems() {
  if (!Device.getAlbumItems.isSupported()) {
    console.warn("当前 Toss App 版本中无法使用。");
    return;
  }

  try {
    const items = await Device.getAlbumItems({
      types: ["PHOTO", "VIDEO"],
      maxCount: 5,
      base64: true,
    });

    if (items.length === 0) {
      console.log("已取消选择。");
      return;
    }
    console.log(items);
  } catch (error) {
    console.error(error);
  }
}

document.getElementById("pick-media").addEventListener("click", pickAlbumItems);
```


---

# 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.getalbumitems.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.
