Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/capacitor-community/media
Capacitor plugin for saving and retrieving photos and videos, and managing photo albums.
https://github.com/capacitor-community/media
angular capacitor capacitor-plugin ionic media plugin
Last synced: 5 days ago
JSON representation
Capacitor plugin for saving and retrieving photos and videos, and managing photo albums.
- Host: GitHub
- URL: https://github.com/capacitor-community/media
- Owner: capacitor-community
- Created: 2019-03-31T18:23:03.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-10-02T02:32:35.000Z (2 months ago)
- Last Synced: 2024-10-12T14:43:46.697Z (2 months ago)
- Topics: angular, capacitor, capacitor-plugin, ionic, media, plugin
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@capacitor-community/media
- Size: 1.6 MB
- Stars: 105
- Watchers: 17
- Forks: 51
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
- awesome-capacitorjs - @capacitor-community/media - Capacitor plugin to activate extra media features. (Plugins / Community Plugins)
- awesome-capacitor - Media - Enable some media features for Capacitor such as create albums, save videos, gifs and more. (Community plugins)
README
Capacitor Media
@capacitor-community/media
Capacitor plugin for saving and retrieving photos and videos, and managing photo albums.## Sponsors
## Maintainers
| Maintainer | GitHub | Social |
| ----------------- | --------------------------------------------- | ----------------------------------------- |
| Nisala Kalupahana | [nkalupahana](https://github.com/nkalupahana) | |
| Stewan Silva | [stewones](https://github.com/stewones) | [@stewones](https://twitter.com/stewones) |## Installation
```bash
npm install @capacitor-community/media
```This plugin is currently for Capacitor 6. Add an `@5` at the end to install for Capacitor 5. Note that the Capacitor 5 version is no longer maintained, and cannot be published to the Google Play Store.
After installing, be sure to sync by running `ionic cap sync`.
## Migrating 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 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`.## Setup
### iOS
You'll need to add the following to your app's `Info.plist` file:
```xml
...
NSPhotoLibraryUsageDescription
Describe why you need access to user's photos (getting albums and media)
NSPhotoLibraryAddUsageDescription
Describe why you need to add photos to user's photo library
...```
### 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:```xml
...
...```
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:```xml
...
...
```You'll also need to add the following configuration to `capacitor.config.ts`:
```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)
* [`getMediaByIdentifier(...)`](#getmediabyidentifier)
* [`getAlbums()`](#getalbums)
* [`savePhoto(...)`](#savephoto)
* [`saveVideo(...)`](#savevideo)
* [`createAlbum(...)`](#createalbum)
* [`getAlbumsPath()`](#getalbumspath)
* [Interfaces](#interfaces)
* [Type Aliases](#type-aliases)
* [Enums](#enums)### getMedias(...)
```typescript
getMedias(options?: MediaFetchOptions | undefined) => Promise
```Get filtered thumbnails from camera roll. iOS only.
[Code Examples](https://github.com/capacitor-community/media/blob/main/example/src/components/GetMedias.tsx)
| Param | Type |
| ------------- | --------------------------------------------------------------- |
| **`options`** |MediaFetchOptions
|**Returns:**
Promise<MediaResponse>
--------------------
### getMediaByIdentifier(...)
```typescript
getMediaByIdentifier(options?: { identifier: string; } | undefined) => Promise
```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](https://github.com/capacitor-community/media/blob/main/example/src/components/GetMedias.tsx)
| Param | Type |
| ------------- | ------------------------------------ |
| **`options`** |{ identifier: string; }
|**Returns:**
Promise<MediaPath>
--------------------
### getAlbums()
```typescript
getAlbums() => Promise
```Get list of albums.
[Code Examples](https://github.com/capacitor-community/media/blob/main/example/src/components/GetAlbums.tsx)
**Returns:**
Promise<MediaAlbumResponse>
--------------------
### savePhoto(...)
```typescript
savePhoto(options?: MediaSaveOptions | undefined) => Promise
```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](https://github.com/capacitor-community/media/blob/main/example/src/components/SaveMedia.tsx)
| Param | Type |
| ------------- | ------------------------------------------------------------- |
| **`options`** |MediaSaveOptions
|**Returns:**
Promise<PhotoResponse>
--------------------
### saveVideo(...)
```typescript
saveVideo(options?: MediaSaveOptions | undefined) => Promise
```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](https://github.com/capacitor-community/media/blob/main/example/src/components/SaveMedia.tsx)
| Param | Type |
| ------------- | ------------------------------------------------------------- |
| **`options`** |MediaSaveOptions
|**Returns:**
Promise<PhotoResponse>
--------------------
### createAlbum(...)
```typescript
createAlbum(options: MediaAlbumCreate) => Promise
```Creates an album.
[Code Examples](https://github.com/capacitor-community/media/blob/main/example/src/components/CreateDemoAlbum.tsx)
| Param | Type |
| ------------- | ------------------------------------------------------------- |
| **`options`** |MediaAlbumCreate
|--------------------
### getAlbumsPath()
```typescript
getAlbumsPath() => Promise
```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](https://github.com/capacitor-community/media/blob/main/example/src/components/CreateDemoAlbum.tsx), [when saving media](https://github.com/capacitor-community/media/blob/main/example/src/components/SaveMedia.tsx)
**Returns:**
Promise<AlbumsPathResponse>
--------------------
### Interfaces
#### MediaResponse
| Prop | Type |
| ------------ | ------------------------- |
| **`medias`** |MediaAsset[]
|#### MediaAsset
| Prop | Type | Description |
| --------------------- | ------------------------------------------------------- | ----------------------------------------------------------------------- |
| **`identifier`** |string
| Platform-specific identifier |
| **`data`** |string
| Data for a photo asset as a base64 encoded string (JPEG only supported) |
| **`creationDate`** |string
| ISO date string for creation date of asset |
| **`duration`** |number
| Duration of asset in seconds, only returned for videos |
| **`fullWidth`** |number
| Full width of original asset |
| **`fullHeight`** |number
| Full height of original asset |
| **`thumbnailWidth`** |number
| Width of thumbnail preview |
| **`thumbnailHeight`** |number
| Height of thumbnail preview |
| **`location`** |MediaLocation
| Location metadata for the asset |#### MediaLocation
| Prop | Type | Description |
| --------------- | ------------------- | ---------------------------------------- |
| **`latitude`** |number
| GPS latitude image was taken at |
| **`longitude`** |number
| GPS longitude image was taken at |
| **`heading`** |number
| Heading of user at time image was taken |
| **`altitude`** |number
| Altitude of user at time image was taken |
| **`speed`** |number
| Speed of user at time image was taken |#### MediaFetchOptions
| Prop | Type | Description |
| ---------------------- | ---------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| **`quantity`** |number
| The 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. |
| **`thumbnailWidth`** |number
| The width of thumbnail to return |
| **`thumbnailHeight`** |number
| The height of thumbnail to return |
| **`thumbnailQuality`** |number
| The quality of thumbnail to return as JPEG (0-100) |
| **`types`** |'photos' \| 'videos' \| 'all'
| Which types of assets to return thumbnails for. |
| **`albumIdentifier`** |string
| Which album identifier to query in (get identifier with getAlbums()) |
| **`sort`** |MediaField \| MediaSort[]
| Sort order of returned assets by field and ascending/descending |#### MediaSort
| Prop | Type |
| --------------- | ------------------------------------------------- |
| **`key`** |MediaField
|
| **`ascending`** |boolean
|#### MediaPath
| Prop | Type | Description |
| ---------------- | ------------------- | -------------------------- |
| **`path`** |string
| Path to media asset |
| **`identifier`** |string
| Identifier for media asset |#### MediaAlbumResponse
| Prop | Type |
| ------------ | ------------------------- |
| **`albums`** |MediaAlbum[]
|#### MediaAlbum
| Prop | Type |
| ---------------- | --------------------------------------------------------- |
| **`identifier`** |string
|
| **`name`** |string
|
| **`type`** |MediaAlbumType
|#### PhotoResponse
| Prop | Type | Description |
| -------------- | ------------------- | -------------------------- |
| **`filePath`** |string
| Available on Android only. |#### MediaSaveOptions
| Prop | Type | Description |
| --------------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| **`path`** |string
| Web URL, base64 encoded URI, or local file path to save. |
| **`albumIdentifier`** |string
| Album 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). |
| **`fileName`** |string
| File name to save the image as in the album. Do not include extension. Android only. |#### MediaAlbumCreate
| Prop | Type |
| ---------- | ------------------- |
| **`name`** |string
|#### AlbumsPathResponse
| Prop | Type |
| ---------- | ------------------- |
| **`path`** |string
|### Type Aliases
#### MediaField
Attributes to sort media by.
[iOS Source](https://developer.apple.com/documentation/photokit/phfetchoptions)
'mediaType' | 'mediaSubtypes' | 'sourceType' | 'pixelWidth' | 'pixelHeight' | 'creationDate' | 'modificationDate' | 'isFavorite' | 'burstIdentifier'
### Enums
#### MediaAlbumType
| Members | Value | Description |
| ------------ | --------------------- | -------------------------------------------------------------- |
| **`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](https://allcontributors.org/docs/en/emoji-key)):
stewones
π» π π§
Zachary Keeton
π»
Pierre Grimaud
π
Talles Alves
π§
Zyad Yasser
π§
Manuel RodrΓguez
π» π§
Michael
π»
Nisala Kalupahana
π» π π‘ π§
Masahiko Sakakibara
π§
ha6-6ru
π»
Stephan Fischer
π»
Matheus Davidson
π» π
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!