6.0.0 • Published 12 days ago

@capacitor-community/media v6.0.0

Weekly downloads
249
License
MIT
Repository
github
Last release
12 days ago

Sponsors

Maintainers

MaintainerGitHubSocial
Nisala Kalupahanankalupahana
Stewan Silvastewones@stewones

Installation

npm install @capacitor-community/media

This plugin is currently for Capacitor 6. Add an @5 at the end to install for Capacitor 5.

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

Migrating to Capacitor 6

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.

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
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())
sort"mediaType" | "mediaSubtypes" | "sourceType" | "pixelWidth" | "pixelHeight" | "creationDate" | "modificationDate" | "isFavorite" | "burstIdentifier" | MediaSort[]Sort order of returned assets by field and ascending/descending

MediaSort

PropType
key"mediaType" | "mediaSubtypes" | "sourceType" | "pixelWidth" | "pixelHeight" | "creationDate" | "modificationDate" | "isFavorite" | "burstIdentifier"
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.

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

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

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

You'll need to add the following to your app's AndroidManifest.xml file:

<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" />
  ...
</manifest>

Note the READ_MEDIA permissions -- these are new in Android 13!

Demo

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

Contributors ✨

Thanks goes to these wonderful people (emoji key):

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

6.0.0

12 days ago

5.4.1

1 month ago

5.4.0

5 months ago

5.3.1

5 months ago

5.2.4

9 months ago

5.2.3

9 months ago

5.2.2

9 months ago

5.3.0

6 months ago

5.2.1

9 months ago

5.2.0

9 months ago

5.1.0

9 months ago

5.2.9

9 months ago

5.2.8

9 months ago

5.2.7

9 months ago

5.2.6

9 months ago

5.2.5

9 months ago

5.0.1

11 months ago

5.0.0

12 months ago

4.2.0

1 year ago

4.1.0

1 year ago

4.0.1

1 year ago

4.0.0-0

2 years ago

3.0.0

2 years ago

1.0.1

4 years ago

1.0.0

4 years ago