expo-media-library
provides access to the user's media library, allowing them to access their existing images and videos from your app, as well as save new ones. You can also subscribe to any updates made to the user's media library.⚠️If your Android app created an album using SDK <= 40 and you want to add more assets to this album, you may need to migrate it to the new scoped directory. Otherwise, your app won't have access to the old album directory and expo-media-library won't be able to add new assets to it. However, all other functions will work without problems. You only need to migrate the old album if you want to add something to it. For more information, check out Android R changes andMediaLibrary.migrateAlbumIfNeededAsync
.
Android Device | Android Emulator | iOS Device | iOS Simulator | Web |
---|---|---|---|---|
→
expo install expo-media-library
If you're installing this in a bare React Native app, you should also follow these additional installation instructions.
expo-media-library
using its built-in config plugin if you use config plugins in your project (EAS Build or expo run:[android|ios]
). The plugin allows you to configure various properties that cannot be set at runtime and require building a new app binary to take effect.expo build:[android|ios]
)expo-media-library
repository.{ "expo": { "plugins": [ [ "expo-media-library", { "photosPermission": "Allow $(PRODUCT_NAME) to access your photos.", "savePhotosPermission": "Allow $(PRODUCT_NAME) to save photos.", "isAccessMediaLocationEnabled": true } ] ] } }
Name | Default | Description |
---|---|---|
photosPermission | "Allow $(PRODUCT_NAME) to access your photos." | iOS only Sets the iOS `NSPhotoLibraryUsageDescription` permission message in Info.plist. |
savePhotosPermission | "Allow $(PRODUCT_NAME) to save photos." | iOS only Sets the iOS `NSPhotoLibraryAddUsageDescription` permission message in Info.plist. |
isAccessMediaLocationEnabled | false | Android only Sets whether or not to request the `ACCESS_MEDIA_LOCATION` permission on Android. |
import * as MediaLibrary from 'expo-media-library';
PermissionHookOptions<{ writeOnly: boolean }>
)Check or request permissions to access the media library.
This uses both requestPermissionsAsync
and getPermissionsAsync
to interact with the permissions.
const [status, requestPermission] = MediaLibrary.usePermissions();
[null | PermissionResponse, RequestPermissionMethod<PermissionResponse>, GetPermissionMethod<PermissionResponse>]
AssetRef | AssetRef[]
) - An array of Asset or their IDs.AlbumRef
) - An Album or its ID.boolean
) - Android only. Whether to copy assets to the new album instead of move them.
Defaults to true
.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.
Promise<boolean>
Returns promise which fulfils with true
if the assets were successfully added to
the album.
Checks if the album should be migrated to a different location. In other words, it checks if the
application has the write permission to the album folder. If not, it returns true
, otherwise false
.
Note: For Android below R, web or iOS, this function always returnsfalse
.
string
) - Name of the album to create.AssetRef
) - An Asset or its ID (required on Android).boolean
) - Android Only. Whether to copy asset to the new album instead of move it.
Defaults to true
.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.
string
) - A URI to the image or video file. It must contain an extension. On Android it
must be a local path, so it must start with file:///
Creates an asset from existing file. The most common use case is to save a picture taken by Camera.
This method requires CAMERA_ROLL
permission.
const { uri } = await Camera.takePictureAsync(); const asset = await MediaLibrary.createAssetAsync(uri);
AlbumRef | AlbumRef[]
) - An array of Album
s or their IDs.boolean
) - iOS Only. Whether to also delete assets belonging to given albums.
Defaults to false
.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
.
Promise<boolean>
Returns a promise which fulfils with true
if the albums were successfully deleted from
the library.
AssetRef | AssetRef[]
) - An array of Asset or their IDs.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.
string
) - Name of the album to look for.Queries for an album with a specific name.
An object representing an Album
, if album with given name exists, otherwise
returns null
.
AlbumsOptions
)Queries for user-created albums in media gallery.
A promise which fulfils with an array of Album
s. Depending on Android version,
root directory of your storage may be listed as album titled "0"
or unlisted at all.
AssetRef
) - An Asset or its ID.MediaLibraryAssetInfoQueryOptions
)Provides more information about an asset, including GPS location, local URI and EXIF metadata.
AssetsOptions
)Fetches a page of assets matching the provided criteria.
iOS Only. Fetches a list of moments, which is a group of assets taken around the same place and time.
boolean
)Checks user's permissions for accessing media library.
Returns whether the Media Library API is enabled on the current device.
Promise<boolean>
A promise which fulfils with a boolean
, indicating whether the Media Library API is
available on the current device.
Moves album content to the special media directories on Android R or above if needed.
Those new locations are in line with the Android scoped storage
- so your application won't
lose write permission to those directories in the future.
This method does nothing if:
The migration is possible when the album contains only compatible files types.
For instance, movies and pictures are compatible with each other, but music and pictures are not.
If automatic migration isn't possible, the function will be rejected.
In that case, you can use methods from the expo-file-system
to migrate all your files manually.
Android R introduced a lot of changes in the storage system. Now applications can't save
anything to the root directory. The only available locations are from the MediaStore
API.
Unfortunately, the media library stored albums in folders for which, because of those changes,
the application doesn't have permissions anymore. However, it doesn't mean you need to migrate
all your albums. If your application doesn't add assets to albums, you don't have to migrate.
Everything will work as it used to. You can read more about scoped storage in the Android documentation.
Available only on iOS >= 14. Allows the user to update the assets that your app has access to.
The system modal is only displayed if the user originally allowed only limited
access to their
media library, otherwise this method is a no-op.
Promise<void>
A promise that either rejects if the method is unavailable (meaning the device is not
running iOS >= 14), or resolves to void
.
Note: This method doesn't inform you if the user changes which assets your app has access to. For that information, you need to subscribe for updates to the user's media library using addListener(listener). IfhasIncrementalChanges
isfalse
, the user changed their permissions.
AssetRef | AssetRef[]
) - An array of Asset or their IDs.AlbumRef
) - An Album or its ID.Removes given assets from album.
On Android, album will be automatically deleted if there are no more assets inside.
Promise<boolean>
Returns promise which fulfils with true
if the assets were successfully removed from
the album.
boolean
)Asks the user to grant permissions for accessing media in user's media library.
string
) - A URI to the image or video file. It must contain an extension. On Android it
must be a local path, so it must start with file:///
.Saves the file at given localUri
to the user's media library. Unlike createAssetAsync()
,
This method doesn't return created asset.
On iOS 11+, it's possible to use this method without asking for CAMERA_ROLL
permission,
however then yours Info.plist
should have NSPhotoLibraryAddUsageDescription
key.
(event: MediaLibraryAssetsChangeEvent) => void
) - A callback that is fired when any assets have been inserted or deleted from the
library, or when the user changes which assets they're allowing access to. On Android it's
invoked with an empty object. On iOS it's invoked with MediaLibraryAssetsChangeEvent
object.Subscribes for updates in user's media library.
An Subscription
object that you can call remove()
on when you would
like to unsubscribe the listener.
Name | Type | Description |
---|---|---|
approximateLocation (optional) | Location | iOS Only. Apply only to albums whose type is 'moment' . Approximated location of all assets in the moment. |
assetCount | number | Estimated number of assets in the album |
endTime | number | iOS Only. Apply only to albums whose type is 'moment' . Latest creation timestamp of all assets in the moment. |
id | string | - |
locationNames (optional) | string[] | iOS Only. Apply only to albums whose type is 'moment' . Names of locations grouped in the moment. |
startTime | number | iOS Only. Apply only to albums whose type is 'moment' . Earliest creation timestamp of all assets in the moment. |
title | string | - |
type (optional) | AlbumType | iOS Only. The type of the assets album. |
string
- Acceptable values are: 'album'
, 'moment'
, 'smartAlbum'
.
Name | Type | Description |
---|---|---|
includeSmartAlbums (optional) | boolean | - |
Name | Type | Description |
---|---|---|
albumId (optional) | string | Android Only. Album ID that the asset belongs to. |
creationTime | number | File creation timestamp. |
duration | number | Duration of the video or audio asset in seconds. |
filename | string | Filename of the asset. |
height | number | Height of the image or video. |
id | string | Internal ID that represents an asset. |
mediaSubtypes (optional) | MediaSubtype[] | iOS Only. An array of media subtypes. |
mediaType | MediaTypeValue | Media type. |
modificationTime | number | Last modification timestamp. |
uri | string | URI that points to the asset. assets://* (iOS), file://* (Android) |
width | number | Width of the image or video. |
Asset
extended by:
Name | Type | Description |
---|---|---|
exif (optional) | object | EXIF metadata associated with the image. |
isFavorite (optional) | boolean | iOS Only. Whether the asset is marked as favorite. |
isNetworkAsset (optional) | boolean | iOS Only. This field is available only if flag shouldDownloadFromNetwork is set to false .
Whether the asset is stored on the network (iCloud on iOS) |
localUri (optional) | string | Local URI for the asset. |
location (optional) | Location | GPS location if available. |
orientation (optional) | number | iOS Only. Display orientation of the image. Orientation is available only for assets whose
mediaType is MediaType.photo . Value will range from 1 to 8, see EXIF orientation specification
for more details. |
Name | Type | Description |
---|---|---|
after (optional) | AssetRef | Asset ID of the last item returned on the previous page. |
album (optional) | AlbumRef | Album or its ID to get assets from specific album. |
createdAfter (optional) | Date | number | Date object or Unix timestamp in milliseconds limiting returned assets only to those that
were created after this date. |
createdBefore (optional) | Date | number | Similarly as createdAfter , but limits assets only to those that were created before specified
date. |
first (optional) | number | The maximum number of items on a single page. Default: `20`
|
mediaType (optional) | MediaTypeValue[] | MediaTypeValue | An array of MediaTypeValues or a single MediaTypeValue .Default: `MediaType.photo`
|
sortBy (optional) | SortByValue[] | SortByValue | An array of SortByValue s or a single SortByValue value. By default, all
keys are sorted in descending order, however you can also pass a pair [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. |
Name | Type | Description |
---|---|---|
latitude | number | - |
longitude | number | - |
Name | Type | Description |
---|---|---|
shouldDownloadFromNetwork (optional) | boolean | Whether allow the asset to be downloaded from network. Only available in iOS with iCloud assets. Default: `true`
|
Name | Type | Description |
---|---|---|
deletedAssets (optional) | Asset[] | Available only if hasIncrementalChanges is true .
Array of Asset s that have been deleted from the library. |
hasIncrementalChanges | boolean | Whether the media library's changes could be described as "incremental changes".
true indicates the changes are described by the insertedAssets , deletedAssets and
updatedAssets values. false indicates that the scope of changes is too large and you
should perform a full assets reload (eg. a user has changed access to individual assets in the
media library). |
insertedAssets (optional) | Asset[] | Available only if hasIncrementalChanges is true .
Array of Asset s that have been inserted to the library. |
updatedAssets (optional) | Asset[] | Available only if hasIncrementalChanges is true .
Array of Asset s that have been updated or completed downloading from network
storage (iCloud on iOS). |
string
- Acceptable values are: 'depthEffect'
, 'hdr'
, 'highFrameRate'
, 'livePhoto'
, 'panorama'
, 'screenshot'
, 'stream'
, 'timelapse'
.
Name | Type | Description |
---|---|---|
audio | 'audio' | - |
photo | 'photo' | - |
unknown | 'unknown' | - |
video | 'video' | - |
string
- Acceptable values are: 'audio'
, 'photo'
, 'video'
, 'unknown'
.
Name | Type | Description |
---|---|---|
assets | T[] | A page of Asset s fetched by the query. |
endCursor | string | ID of the last fetched asset. It should be passed as after option in order to get the
next page. |
hasNextPage | boolean | Whether there are more assets to fetch. |
totalCount | number | Estimated total number of assets that match the query. |
Acceptable values are: 'never'
, number
.
Acceptable values are: PermissionHookBehavior
, Options
.
EXPermissionResponse
extended by:
Name | Type | Description |
---|---|---|
accessPrivileges (optional) | 'all' | 'limited' | 'none' | Indicates if your app has access to the whole or only part of the photo library. Possible values are:
|
string
- Acceptable values are: 'default'
, 'mediaType'
, 'width'
, 'height'
, 'creationTime'
, 'modificationTime'
, 'duration'
.
Name | Type | Description |
---|---|---|
creationTime | 'creationTime' | - |
default | 'default' | - |
duration | 'duration' | - |
height | 'height' | - |
mediaType | 'mediaType' | - |
modificationTime | 'modificationTime' | - |
width | 'width' | - |
Name | Type | Description |
---|---|---|
remove | () => void | A method to unsubscribe the listener. |
Name | Type | Description |
---|---|---|
canAskAgain | boolean | - |
expires | PermissionExpiration | - |
granted | boolean | - |
status | PermissionStatus | - |
DENIED
PermissionStatus.DENIED = "denied"
GRANTED
PermissionStatus.GRANTED = "granted"
UNDETERMINED
PermissionStatus.UNDETERMINED = "undetermined"
AndroidManifest.xml
.Android Permission | Description |
---|---|
Allows an application to read from external storage. | |
Allows an application to write to external storage. |
Info.plist Key | Description |
---|---|
A message that tells the user why the app is requesting access to the user’s photo library. | |
A message that tells the user why the app is requesting add-only access to the user’s photo library. |