Provides access to user's media library.
Requires Permissions.CAMERA_ROLL
permissions.
Note: Not compatible with web.
import * as MediaLibrary from 'react-native-media-library';
MediaLibrary.requestPermissionsAsync()
Asks the user to grant permissions for accessing media in user's media library. Alias for Permissions.askAsync(Permissions.CAMERA_ROLL)
.
Returns a promise that resolves to an object of type PermissionsResult.
MediaLibrary.getPermissionsAsync()
Checks user's permissions for accessing media library. Alias for Permissions.getAsync(Permissions.CAMERA_ROLL)
.
Returns a promise that resolves to an object of type PermissionsResult.
MediaLibrary.createAssetAsync(localUri)
Creates an asset from existing file. The most common use case is to save a picture taken by Camera.
const { uri } = await Camera.takePictureAsync();
const asset = await MediaLibrary.createAssetAsync(uri);
file:///
.An object representing an asset.
MediaLibrary.getAssetsAsync(options)
Fetches a page of assets matching the provided criteria.
options (object)
[key, ascending]
where the second item is a boolean
value that means whether to use ascending order. Note that if the SortBy.default
key is used, then ascending
argument will not matter.
Earlier items have higher priority when sorting out the results.
If empty, this method will use the default sorting that is provided by the platform.MediaType.photo
is set.createdAfter
, but limits assets only to those that were created before specified date.A promise that resolves to an object that contains following keys:
after
option in order to get the next page.MediaLibrary.getAssetInfoAsync(asset)
Provides more informations about an asset, including GPS location, local URI and EXIF metadata.
Asset object extended by additional fields listed in the table.
MediaLibrary.deleteAssetsAsync(assets)
Deletes assets from the library. On iOS it deletes assets from all albums they belong to, while on Android it keeps all copies of them (album is strictly connected to the asset). Also, there is additional dialog on iOS that requires user to confirm this action.
Returns true
if the assets were successfully deleted.
MediaLibrary.getAlbumsAsync()
Queries for user-created albums in media gallery.
An array of albums.
MediaLibrary.getAlbumAsync(albumName)
Queries for an album with a specific name.
An object representing an album if album with given name exists, otherwise returns null
.
MediaLibrary.createAlbumAsync(albumName, asset, copyAsset)
Creates an album with given name and initial asset.
The asset parameter is required on Android, since it's not possible to create empty album on this platform.
On Android, by default it copies given asset from the current album to the new one, however it's also possible to move it by passing false
as copyAsset
argument.
In case it's copied you should keep in mind that getAssetsAsync
will return duplicated asset.
true
. (Android only)Newly created album.
MediaLibrary.deleteAlbumsAsync(albums, deleteAssets)
Deletes given albums from the library.
On Android by default it deletes assets belonging to given albums from the library. On iOS it doesn't delete these assets, however it's possible to do by passing true
as deleteAssets
.
false
. (iOS only)Returns a promise resolving to true
if the albums were successfully deleted from the library.
MediaLibrary.addAssetsToAlbumAsync(assets, album, copyAssets)
Adds array of assets to the album.
On Android, by default it copies assets from the current album to provided one, however it's also possible to move them by passing false
as copyAssets
argument.
In case they're copied you should keep in mind that getAssetsAsync
will return duplicated assets.
true
. (Android only)Resolves to true
if the assets were successfully added to the album.
MediaLibrary.removeAssetsFromAlbumAsync(assets, album)
Removes given assets from album.
On Android, album will be automatically deleted if there are no more assets inside.
Returns true
if the assets were successfully removed from the album.
MediaLibrary.getMomentsAsync()
Available on iOS only. Fetches a list of moments, which is a group of assets taken around the same place and time.
An array of albums whose type is moment
.
MediaLibrary.addListener(listener)
Subscribes for updates in user's media library.
An EventSubscription object that you can call remove()
on when you would like to unsubscribe the listener.
MediaLibrary.removeAllListeners()
Removes all listeners.
PermissionsResult
Field name | Type | Description |
---|---|---|
status | string | Permission status with possible values: granted , denied , undetermined . |
granted | boolean | Boolean value meaning whether the permission is granted or not. |
Asset
Field name | Type | Platforms | Description | Possible values |
---|---|---|---|---|
id | string | both | Internal ID that represents an asset | |
filename | string | both | Filename of the asset | |
uri | string | both | URI that points to the asset | assets://* (iOS), file://* (Android) |
mediaType | string | both | Media type | MediaType.audio , MediaType.photo , MediaType.video , MediaType.unknown |
width | number | both | Width of the image or video | |
height | number | both | Height of the image or video | |
creationTime | number | both | File creation timestamp | |
modificationTime | number | both | Last modification timestamp | |
duration | number | both | Duration of the video or audio asset | |
mediaSubtypes | array | iOS | An array of media subtypes | hdr , panorama , stream , timelapse , screenshot , highFrameRate , livePhoto , depthEffect |
albumId | string | Android | Album ID that the asset belongs to | |
localUri * | string | both | Local URI for the asset | |
location * | object | both | GPS location if available | latitude: number, longitude: number or null |
exif * | object | both | EXIF metadata associated with the image | |
orientation * | number | iOS | Display orientation of the image | Numbers 1-8, see EXIF orientation specification |
isFavorite * | boolean | iOS | Whether the asset is marked as favorite | true , false |
* These fields can be obtained only by calling
getAssetInfoAsync
method
Album
Field name | Type | Platforms | Description | Possible values |
---|---|---|---|---|
id | string | both | ||
title | string | both | ||
assetCount | number | both | Estimated number of assets in the album | |
type | string | iOS | The type of the assets album | album , moment , smartAlbum |
startTime * | number | iOS | Earliest creation timestamp of all assets in the moment | |
endTime * | number | iOS | Latest creation timestamp of all assets in the moment | |
approximateLocation * | object | iOS | Approximated location of all assets in the moment | latitude: number, longitude: number or null |
locationNames * | array | iOS | Names of locations grouped in the moment |
* These fields apply only to albums whose type is
moment
MediaLibrary.MediaType
Possible media types:
MediaType.photo
MediaType.video
MediaType.audio
MediaType.unknown
MediaLibrary.SortBy
Supported keys that can be used to sort getAssetsAsync
results:
SortBy.default
SortBy.creationTime
SortBy.modificationTime
SortBy.mediaType
SortBy.width
SortBy.height
SortBy.duration