Getting Started
Questo contenuto non è ancora disponibile nella tua lingua.
-
Install the plugin
Terminal window npm i @capgo/capacitor-photo-libraryTerminal window pnpm add @capgo/capacitor-photo-libraryTerminal window yarn add @capgo/capacitor-photo-libraryTerminal window bun add @capgo/capacitor-photo-library -
Sync platforms
Terminal window npx cap syncTerminal window pnpm cap syncTerminal window yarn cap syncTerminal window bunx cap sync
Platform permissions
- iOS: Update
Info.plistwithNSPhotoLibraryUsageDescription(andNSPhotoLibraryAddUsageDescriptionif you plan to save media). - Android: For Android 13+, declare the photo/video permissions (
READ_MEDIA_IMAGES,READ_MEDIA_VIDEO). Capacitor 5 handles this automatically, but double-check custom manifests.
Request access
import { PhotoLibrary } from '@capgo/capacitor-photo-library';
const { state } = await PhotoLibrary.requestAuthorization();if (state !== 'authorized' && state !== 'limited') { throw new Error('Photo access denied');}Load the gallery
import { Capacitor } from '@capacitor/core';
const { assets, hasMore, nextPage } = await PhotoLibrary.getLibrary({ limit: 100, includeAlbums: false, thumbnailWidth: 256, thumbnailHeight: 256,});
const gallery = assets.map((asset) => ({ id: asset.id, fileName: asset.fileName, created: asset.creationDate, thumbnailUrl: asset.thumbnail ? asset.thumbnail.webPath ?? Capacitor.convertFileSrc(asset.thumbnail.path) : undefined, photoUrl: asset.file ? asset.file.webPath ?? Capacitor.convertFileSrc(asset.file.path) : undefined,}));To fetch additional pages, call PhotoLibrary.getLibrary({ cursor: nextPage }).
Let users pick media
const selection = await PhotoLibrary.pickMedia({ selectionLimit: 5, includeVideos: true,});
console.log('User selected', selection.assets.length, 'items');Clean up cached files
Generated thumbnails and exports are stored in the app cache directory under photoLibrary. Remove them with your preferred file API when you need to reclaim storage.