快速入门
复制一条配置 prompt,包含安装步骤和该插件的完整 Markdown 指南。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-photo-library`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/photo-library/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
你可以使用我们的 AI 辅助设置来安装插件。使用以下命令将 Capgo skills 添加到你的 AI 工具中:
bunx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins然后使用以下 prompt:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-photo-library` plugin in my project.如果你更倾向于手动设置,请运行以下命令安装插件,并参考下文针对各平台的具体说明:
bun add @capgo/capacitor-photo-librarybunx cap sync导入
“导入”章节import { PhotoLibrary } from '@capgo/capacitor-photo-library';快速入门
“快速开始”章节import { PhotoLibrary } from '@capgo/capacitor-photo-library';
const result = await PhotoLibrary.checkAuthorization();console.log(result);示例:requestAuthorization
标题为 “示例:requestAuthorization” 的章节import { PhotoLibrary } from '@capgo/capacitor-photo-library';
const result = await PhotoLibrary.requestAuthorization();console.log(result);示例:getAlbums
标题为 “示例:getAlbums” 的章节import { PhotoLibrary } from '@capgo/capacitor-photo-library';
const result = await PhotoLibrary.getAlbums();console.log(result);常见坑点
标题为“常见坑点”的章节- 原生同步: 运行
bunx cap sync在插件升级后。 - 单一事实来源: 信任
src/definitions.ts在 GitHub 仓库中。
API 概览
“API 概览”章节checkAuthorization
“checkAuthorization” 章节返回当前的授权状态,不会弹出用户提示。
import { PhotoLibrary } from '@capgo/capacitor-photo-library';
const result = await PhotoLibrary.checkAuthorization();console.log(result);requestAuthorization
“requestAuthorization” 章节根据需要请求访问相册的权限。
import { PhotoLibrary } from '@capgo/capacitor-photo-library';
const result = await PhotoLibrary.requestAuthorization();console.log(result);getAlbums
标题为 “getAlbums” 的章节获取可用相册。
import { PhotoLibrary } from '@capgo/capacitor-photo-library';
const result = await PhotoLibrary.getAlbums();console.log(result);getLibrary
标题为 “getLibrary” 的章节获取库中的资源以及可在 Web View 中显示的 URL。
import { PhotoLibrary } from '@capgo/capacitor-photo-library';
const result = await PhotoLibrary.getLibrary();console.log(result);getPhotoUrl
标题为 “getPhotoUrl” 的章节获取资源全分辨率版本的可显示 URL。
如果你已经调用了 getLibrary 带 includeFullResolutionData,通常不需要使用此方法。
import { PhotoLibrary } from '@capgo/capacitor-photo-library';
const result = await PhotoLibrary.getPhotoUrl({ id: 'id-123' });console.log(result);getThumbnailUrl
“getThumbnailUrl” 章节获取该资源缩略图的可显示 URL。
import { PhotoLibrary } from '@capgo/capacitor-photo-library';
const result = await PhotoLibrary.getThumbnailUrl({ id: 'id-123' });console.log(result);pickMedia
“pickMedia” 章节打开原生系统选择器,使用户无需授予完整的相册访问权限即可选择媒体文件。 所选文件将被复制到应用缓存中,并以可移植 URL 的形式返回。
import { PhotoLibrary } from '@capgo/capacitor-photo-library';
const result = await PhotoLibrary.pickMedia();console.log(result);类型参考
“类型参考” 章节PhotoLibraryAuthorizationState
“PhotoLibraryAuthorizationState” 章节export type PhotoLibraryAuthorizationState = 'authorized' | 'limited' | 'denied' | 'notDetermined';PhotoLibraryAlbum
“PhotoLibraryAlbum” 章节export interface PhotoLibraryAlbum { id: string; title: string; assetCount: number;}GetLibraryOptions
“GetLibraryOptions” 章节export interface GetLibraryOptions { /** * Number of assets to skip from the beginning of the query. */ offset?: number; /** * Maximum number of assets to return. Omit to return everything that matches. */ limit?: number; /** * Include images in the result. Defaults to `true`. */ includeImages?: boolean; /** * Include videos in the result. Defaults to `false`. */ includeVideos?: boolean; /** * Include information about the albums each asset belongs to. Defaults to `false`. */ includeAlbumData?: boolean; /** * Include assets stored in the cloud (iCloud / Google Photos). Defaults to `true`. */ includeCloudData?: boolean; /** * If `true`, use the original filenames reported by the OS when available. */ useOriginalFileNames?: boolean; /** * Width of the generated thumbnails. Defaults to `512`. */ thumbnailWidth?: number; /** * Height of the generated thumbnails. Defaults to `384`. */ thumbnailHeight?: number; /** * JPEG quality for generated thumbnails (0-1). Defaults to `0.5`. */ thumbnailQuality?: number; /** * When `true`, copies the full sized asset into the app cache and returns its URL. * Defaults to `false`. */ includeFullResolutionData?: boolean;}GetLibraryResult
“GetLibraryResult” 章节export interface GetLibraryResult { assets: PhotoLibraryAsset[]; /** * Total number of assets matching the query in the library. `assets.length` can be less * than this value when pagination is used. */ totalCount: number; /** Whether more assets are available when using pagination. */ hasMore: boolean;}PhotoLibraryFile
“PhotoLibraryFile” 章节export interface PhotoLibraryFile { /** Absolute path on the native file system. */ path: string; /** * URL that can be used inside a web view. Usually produced by `Capacitor.convertFileSrc(path)`. */ webPath: string; mimeType: string; /** Size in bytes if known, otherwise `-1`. */ size: number;}PickMediaOptions
“PickMediaOptions” 章节export interface PickMediaOptions { /** * Maximum number of items the user can select. Use `0` to allow unlimited selection. * Defaults to `1`. */ selectionLimit?: number; /** Allow the user to select images. Defaults to `true`. */ includeImages?: boolean; /** Allow the user to select videos. Defaults to `false`. */ includeVideos?: boolean; /** Width of the generated thumbnails for picked items. Defaults to `256`. */ thumbnailWidth?: number; /** Height of the generated thumbnails for picked items. Defaults to `256`. */ thumbnailHeight?: number; /** JPEG quality for generated thumbnails (0-1). Defaults to `0.7`. */ thumbnailQuality?: number;}PickMediaResult
“PickMediaResult” 章节export interface PickMediaResult { assets: PhotoLibraryAsset[];}PhotoLibraryAsset
“PhotoLibraryAsset” 章节export interface PhotoLibraryAsset { id: string; fileName: string; type: PhotoAssetType; width: number; height: number; duration?: number; creationDate?: string; modificationDate?: string; latitude?: number; longitude?: number; mimeType: string; /** Size in bytes reported by the OS for the underlying asset, if available. */ size?: number; albumIds?: string[]; thumbnail?: PhotoLibraryFile; file?: PhotoLibraryFile;}PhotoAssetType
标题为 “PhotoAssetType” 的章节export type PhotoAssetType = 'image' | 'video';唯一数据源
“唯一可信来源”章节本页面由该插件的 src/definitions.ts当上游的公共 API 发生变化时,请重新运行 sync。
继续阅读入门指南
标题为“从入门指南继续”的章节如果你正在使用 快速入门 来规划 dashboard 和 API 操作,请将其连接至 使用 @capgo/capacitor-photo-library 在“使用 @capgo/capacitor-photo-library”中实现原生能力, API 概览 关于 API 概览中的实现细节, 简介 有关 Introduction 中的实现细节, API 密钥 有关 API Keys 中的实现细节,以及 设备 有关 Devices 中的实现细节。