@capacitor-community/media

中文文档
9.1.0发布于 2026/03/27MITTypeScriptESM + CJS零依赖近 30 天下载 31.7万

Capacitor plugin for saving and retrieving photos and videos, and managing photo albums.

原始使用文档
文档目录21 节
  1. Capacitor Media
  2. Maintainers
  3. Installation
  4. Migrating from Plugin v6 to v7+
  5. Migrating from Plugin v5 to v6
  6. Setup
  7. iOS
  8. Android
  9. Demo
  10. API
  11. getMedias(...)
  12. getMediaByIdentifier(...)
  13. getAlbums()
  14. savePhoto(...)
  15. saveVideo(...)
  16. createAlbum(...)
  17. getAlbumsPath()
  18. Interfaces
  19. Type Aliases
  20. Enums
  21. Contributors ✨


Capacitor Media

@capacitor-community/media

Capacitor plugin for saving and retrieving photos and videos, and managing photo albums.


Maintainers

MaintainerGitHubSocial
Nisala Kalupahanankalupahana
Stewan Silvastewones@stewones

Installation

npm install @capacitor-community/media

This plugin is currently maintained for Capacitor 8 (plugin version 9). Add an @8 at the end to install for Capacitor 7, or @7 for Capacitor 6. There are currently no API changes between these three versions, so if you're migrating between plugin v7 - v9 (Capacitor 6 - 8), no code changes should be required. SPM is only supported for plugin v9.

After installing, be sure to sync by running npx cap sync.

Migrating from Plugin v6 to v7+

There has been a breaking change to the way permissions are handled on Android.

  • If your app is not a "gallery" app (does not need access to all photo albums on the device, just its own), you no longer need the READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE, READ_MEDIA_IMAGES, or READ_MEDIA_VIDEO permissions. You can remove them from your AndroidManifest.xml file. The plugin will no longer request any permissions to save images to your app's albums.
  • If your app is a gallery app and you need access to all albums on the device, you'll need to update your capacitor.config.ts file (and keep all of the above permissions) -- see the setup instructions below.

You will need to update to v7 to publish an app with this plugin on the Google Play Store.

Migrating from Plugin v5 to v6

There are a few breaking changes to take note of:

  • saveGif no longer exists. Use savePhoto for images and GIFs.
  • Error text has been changed. If you were checking for specific error messages, you should now use error.code, which will be accessDenied, argumentError, downloadError, or filesystemError.

Setup

iOS

You'll need to add the following to your app's Info.plist file:

<dict>
  ...
  <key>NSPhotoLibraryUsageDescription</key>
  <string>Describe why you need access to user's photos (getting albums and media)</string>
  <key>NSPhotoLibraryAddUsageDescription</key>
  <string>Describe why you need to add photos to user's photo library</string>
  ...
</dict>

Android

By default, this plugin only has access to the albums associated with the app it's installed in. This should be enough for most use cases. For this basic level of access, you only need to add the following permission:

<manifest>
  ...
  <uses-permission android:name="android.permission.INTERNET" />
  ...
</manifest>

If you're building an app that needs to access all photos and videos on the device (e.g. a photo gallery app), you'll need the following permissions:

<manifest>
  ...
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
  <uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
  ...

You'll also need to add the following configuration to capacitor.config.ts:

plugins: {
  Media: {
    androidGalleryMode: true
  }
}

You can find an example of how this should look in the example/ folder.

Demo

Go the the example/ folder to play with an example app that should show all functionality of this plugin.

API

Unless otherwise noted, there should be full feature parity between iOS and Android. Web is not supported.

getMedias(...)

getMedias(options?: MediaFetchOptions | undefined) => Promise<MediaResponse>

Get filtered thumbnails from camera roll. iOS only.

Code Examples

ParamType
optionsMediaFetchOptions

Returns: Promise<MediaResponse>


getMediaByIdentifier(...)

getMediaByIdentifier(options?: { identifier: string; } | undefined) => Promise<MediaPath>

Get a filesystem path to a full-quality media asset by its identifier. iOS only. This is not included for Android because on Android, a media asset's identifier IS its path! You can simply use the Filesystem plugin to work with it. On iOS, you have to turn the identifier into a path using this function. After that, you can use the Filesystem plugin, same as Android.

Code Examples

ParamType
options{ identifier: string; }

Returns: Promise<MediaPath>


getAlbums()

getAlbums() => Promise<MediaAlbumResponse>

Get list of albums.

Code Examples

Returns: Promise<MediaAlbumResponse>


savePhoto(...)

savePhoto(options?: MediaSaveOptions | undefined) => Promise<PhotoResponse>

Saves a still photo or GIF to the camera roll.

On Android and iOS, this supports web URLs, base64 encoded images (e.g. data:image/jpeg;base64,...), and local files. On Android, all image formats supported by the user's photo viewer are supported. On iOS, most common image formats are supported.

Code Examples

ParamType
optionsMediaSaveOptions

Returns: Promise<PhotoResponse>


saveVideo(...)

saveVideo(options?: MediaSaveOptions | undefined) => Promise<PhotoResponse>

Saves a video to the camera roll.

On Android and iOS, this supports web URLs, base64 encoded videos (e.g. data:image/mp4;base64,...), and local files. On Android, all video formats supported by the user's photo viewer are supported. On iOS, the supported formats are based on whatever iOS supports at the time.

Code Examples

ParamType
optionsMediaSaveOptions

Returns: Promise<PhotoResponse>


createAlbum(...)

createAlbum(options: MediaAlbumCreate) => Promise<void>

Creates an album.

Code Examples

ParamType
optionsMediaAlbumCreate

getAlbumsPath()

getAlbumsPath() => Promise<AlbumsPathResponse>

Gets the path where album folders and their corresponding photos are stored on the Android filesystem. This can be used to identify your album by more than just its name on Android, in case there are multiple albums with the same name, which is possible on Android. Just compare the albums path to the start of the album identifier when getting albums.

Only available on Android.

Code Examples: basic, when saving media

Returns: Promise<AlbumsPathResponse>


Interfaces

MediaResponse

PropType
mediasMediaAsset[]

MediaAsset

PropTypeDescription
identifierstringPlatform-specific identifier
datastringData for a photo asset as a base64 encoded string (JPEG only supported)
creationDatestringISO date string for creation date of asset
durationnumberDuration of asset in seconds, only returned for videos
fullWidthnumberFull width of original asset
fullHeightnumberFull height of original asset
thumbnailWidthnumberWidth of thumbnail preview
thumbnailHeightnumberHeight of thumbnail preview
locationMediaLocationLocation metadata for the asset

MediaLocation

PropTypeDescription
latitudenumberGPS latitude image was taken at
longitudenumberGPS longitude image was taken at
headingnumberHeading of user at time image was taken
altitudenumberAltitude of user at time image was taken
speednumberSpeed of user at time image was taken

MediaFetchOptions

PropTypeDescription
quantitynumberThe number of photos to fetch, sorted by last created date descending. To paginate, just request a higher quantity -- OS caching should make this relatively performant.
thumbnailWidthnumberThe width of thumbnail to return
thumbnailHeightnumberThe height of thumbnail to return
thumbnailQualitynumberThe quality of thumbnail to return as JPEG (0-100)
types'photos' | 'videos' | 'all'Which types of assets to return thumbnails for.
albumIdentifierstringWhich album identifier to query in (get identifier with getAlbums())
sortMediaField | MediaSort[]Sort order of returned assets by field and ascending/descending

MediaSort

PropType
keyMediaField
ascendingboolean

MediaPath

PropTypeDescription
pathstringPath to media asset
identifierstringIdentifier for media asset

MediaAlbumResponse

PropType
albumsMediaAlbum[]

MediaAlbum

PropType
identifierstring
namestring
typeMediaAlbumType

PhotoResponse

PropTypeDescription
filePathstringAvailable on Android only.
identifierstringAvailable on iOS only. To get a file path for an image on iOS, pass this identifier to getMediaByIdentifier.

MediaSaveOptions

PropTypeDescription
pathstringWeb URL, base64 encoded URI, or local file path to save.
albumIdentifierstringAlbum identifier from getAlbums(). Since 5.0, identifier is used on both Android and iOS. Identifier is required on Android but not on iOS. On iOS 14+, if the identifier is not specified and no permissions have been requested yet, add-only permissions will be requested instead of full permissions (assuming NSPhotoLibraryAddUsageDescription is in Info.plist).
fileNamestringFile name to save the image as in the album. Do not include extension. Android only.

MediaAlbumCreate

PropType
namestring

AlbumsPathResponse

PropType
pathstring

Type Aliases

MediaField

Attributes to sort media by.

iOS Source

'mediaType' | 'mediaSubtypes' | 'sourceType' | 'pixelWidth' | 'pixelHeight' | 'creationDate' | 'modificationDate' | 'isFavorite' | 'burstIdentifier'

Enums

MediaAlbumType

MembersValueDescription
Smart'smart'Album is a "smart" album (such as Favorites or Recently Added)
Shared'shared'Album is a cloud-shared album
User'user'Album is a user-created album

Contributors ✨

Thanks goes to these wonderful people (emoji key):

stewones
stewones

💻 📖 🚧
Zachary Keeton
Zachary Keeton

💻
Pierre Grimaud
Pierre Grimaud

📖
Talles Alves
Talles Alves

🚧
Zyad Yasser
Zyad Yasser

🚧
Manuel Rodríguez
Manuel Rodríguez

💻 🚧
Michael
Michael

💻
Nisala Kalupahana
Nisala Kalupahana

💻 📖 💡 🚧
Masahiko Sakakibara
Masahiko Sakakibara

🚧
ha6-6ru
ha6-6ru

💻
Stephan Fischer
Stephan Fischer

💻
Matheus Davidson
Matheus Davidson

💻 📖
Maurice Wegner
Maurice Wegner

💻
Samuel Ramos Muñoz
Samuel Ramos Muñoz

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

版本与发布记录

@capacitor-community/media 共发布 33 个版本,首次发布于 2020/06/28;过去 12 个月发布了 4 个版本。

版本发布日期距今
9.1.0latest2026/03/277 个月前
9.0.22026/02/278 个月前
9.0.12026/01/189 个月前
9.0.02025/12/3110 个月前
8.0.12025/03/181.6 年前
8.0.02025/02/071.8 年前

关于 @capacitor-community/media 的常见问题

@capacitor-community/media 是什么?

Capacitor plugin for saving and retrieving photos and videos, and managing photo albums.

如何安装 @capacitor-community/media?

在项目目录运行 npm install @capacitor-community/media,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@capacitor/core(>=8.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@capacitor-community/media 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@capacitor-community/media 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@capacitor-community/media 可以商用吗?

@capacitor-community/media 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@capacitor-community/media 的下载量有多少?

根据 npm 官方下载统计,@capacitor-community/media 在 2026/10/09 – 2026/11/07 这 30 天内被下载 317,460 次,日均约 10,582 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@capacitor-community/media 还在维护吗?

最新版本 9.1.0 发布于 2026/03/27(7 个月前);至今共发布 33 个版本,其中过去 12 个月发布了 4 个;首次发布于 2020/06/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。