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 and MediaLibrary.migrateAlbumIfNeededAsync.
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.
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 MediaLibrary.addListener(listener). If hasIncrementalChanges is false, the user changed their permissions.
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:
app is running on iOS, web or Android below R
app has write permission to the album folder
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.
first (number) -- The maximum number of items on a single page. Defaults to 20.
after (string) -- Asset ID of the last item returned on the previous page.
album (string | Album) -- Album or its ID to get assets from specific album.
sortBy (array) -- An array of SortBy keys. 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.
mediaType (array) -- An array of MediaType types. By default MediaType.photo is set.
createdAfter (Date | number) -- Date object or Unix timestamp in milliseconds limiting returned assets only to those that were created after this date.
createdBefore (Date | number) -- Similarly as createdAfter, but limits assets only to those that were created before specified date.
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.
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.
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.
listener (function) -- 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 an object containing following keys:
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).
Available only if hasIncrementalChanges is true:
insertedAssets (array) -- Array of assets that have been inserted to the library.
deletedAssets (array) -- Array of assets that have been deleted from the library.
updatedAssets (array) -- Array of assets that have been updated or completed downloading from network storage (iCloud in iOS).