Photo Library
The PhotoLibrary plugin allows access to photos from device by url. So you can use plain img tag to display photos and their thumbnails, and different 3rd party libraries as well. Saving photos and videos to the library is also supported. cdvphotolibrary urls should be trusted by Angular. See plugin homepage to learn how.
Repo: https://github.com/terikon/cordova-plugin-photo-library
Installation
- Install the Cordova and Ionic Native plugins:
$ ionic cordova plugin add cordova-plugin-photo-library --variable PHOTO_LIBRARY_USAGE_DESCRIPTION="To choose photos" $ npm install --save @ionic-native/photo-library@4
- Add this plugin to your app's module
Supported platforms
- Android
- Browser
- iOS
Usage
import { PhotoLibrary } from '@ionic-native/photo-library';
constructor(private photoLibrary: PhotoLibrary) { }
this.photoLibrary.requestAuthorization().then(() => {
this.photoLibrary.getLibrary().subscribe({
next: library => {
library.forEach(function(libraryItem) {
console.log(libraryItem.id); // ID of the photo
console.log(libraryItem.photoURL); // Cross-platform access to photo
console.log(libraryItem.thumbnailURL);// Cross-platform access to thumbnail
console.log(libraryItem.fileName);
console.log(libraryItem.width);
console.log(libraryItem.height);
console.log(libraryItem.creationDate);
console.log(libraryItem.latitude);
console.log(libraryItem.longitude);
console.log(libraryItem.albumIds); // array of ids of appropriate AlbumItem, only of includeAlbumsData was used
});
},
error: err => { console.log('could not get photos'); },
complete: () => { console.log('done getting photos'); }
});
})
.catch(err => console.log('permissions weren\'t granted'));
Instance Members
getLibrary(options)
Retrieves library items. Library item contains photo metadata like width and height, as well as photoURL and thumbnailURL.
Param | Type | Details |
---|---|---|
options |
GetLibraryOptions
|
Optional, like thumbnail size and chunks settings. |
Returns: Observable<LibraryItem[]>
Returns library items. If appropriate option was set, will be returned by chunks.
requestAuthorization(options)
Asks user permission to access photo library.
Param | Type | Details |
---|---|---|
options |
RequestAuthorizationOptions
|
Optional, like whether only read access needed or read/write. |
Returns: Promise<void>
Returns a promise that resolves when permissions are granted, and fails when not.
getAlbums()
Returns list of photo albums on device.
Returns: Promise<AlbumItem[]>
Resolves to list of albums.
getThumbnailURL(photo, options)
Provides means to request URL of thumbnail, with specified size or quality.
Param | Type | Details |
---|---|---|
photo |
string |LibraryItem
|
Id of photo, or LibraryItem. |
options |
GetThumbnailOptions
|
Options, like thumbnail size or quality. |
Returns: Promise<string>
Resolves to URL of cdvphotolibrary schema.
getPhotoURL(photo, options)
Provides means to request photo URL by id.
Param | Type | Details |
---|---|---|
photo |
string |LibraryItem
|
Id or LibraryItem. |
options |
GetPhotoOptions
|
Optional options. |
Returns: Promise<string>
Resolves to URL of cdvphotolibrary schema.
getThumbnail(photo, options)
Returns thumbnail as Blob.
Param | Type | Details |
---|---|---|
photo |
string |LibraryItem
|
Id or LibraryItem. |
options |
GetThumbnailOptions
|
Options, like thumbnail size or quality. |
Returns: Promise<Blob>
Resolves requested thumbnail as blob.
getPhoto(photo, options)
Returns photo as Blob.
Param | Type | Details |
---|---|---|
photo |
string |LibraryItem
|
Id or LibraryItem. |
options |
GetPhotoOptions
|
Optional options. |
Returns: Promise<Blob>
Resolves requested photo as blob.
saveImage(url, album, options)
Saves image to specified album. Album will be created if not exists. LibraryItem that represents saved image is returned.
Param | Type | Details |
---|---|---|
url |
string
|
URL of a file, or DataURL. |
album |
AlbumItem |string
|
Name of an album or AlbumItem object. |
options |
GetThumbnailOptions
|
Options, like thumbnail size for resulting LibraryItem. |
Returns: Promise<LibraryItem>
Resolves to LibraryItem that represents saved image.
saveVideo(url, album)
Saves video to specified album. Album will be created if not exists.
Param | Type | Details |
---|---|---|
url |
string
|
URL of a file, or DataURL. |
album |
AlbumItem |string
|
Name of an album or AlbumItem object. |
Returns: Promise<void>
Resolves when save operation completes.