Media Library
Adds image and video files to the Photos app on iOS and to the gallery on Android. Files are saved byte for byte, with no re-encoding. Permission helpers report full or limited access.
- pyxpo-media-library
- v0.1.0a1
- Native: Swift + Kotlin
- Like expo-media-library
Install
Install the package into the same environment as pyxpo.
Add media-library to modules in your app config, then import it in your code.
[tool.pyxpo]
modules = ["media-library"]from pyxpo_media_library import MediaLibraryThis module has native code. After you add it, run pyxpo run or pyxpo build again so it is compiled into the app. Hot reload alone is not enough.
Platforms
Quick start
from pyxpo_media_library import MediaLibrary
async def save(uri):
perm = await MediaLibrary.requestPermissionsAsync(writeOnly=True)
if perm["granted"]:
await MediaLibrary.saveToLibraryAsync(uri)Configuration
These options set the text iOS shows when it asks the user for access. Each has a default, so you only set the ones you want to change.
| Option | Info.plist key | Default |
|---|---|---|
addUsageDescription | NSPhotoLibraryAddUsageDescription | Save photos and videos to your photo library. |
usageDescription | NSPhotoLibraryUsageDescription | Access the photos and videos in your library. |
[tool.pyxpo.module_options.media-library]
addUsageDescription = "Save photos and videos to your photo library."
usageDescription = "Access the photos and videos in your library."Permissions
pyxpo adds these to Info.plist and AndroidManifest.xml for you when the module is listed.
| iOS | NSPhotoLibraryAddUsageDescription to add. NSPhotoLibraryUsageDescription for full access. |
|---|---|
| Android | Adding: none on Android 10+, WRITE_EXTERNAL_STORAGE up to Android 9. Reading: READ_MEDIA_IMAGES, READ_MEDIA_VIDEO and READ_MEDIA_VISUAL_USER_SELECTED on 13+, READ_EXTERNAL_STORAGE before. |
API reference
MediaLibrary
All methods are coroutines. snake_case names also work: save_to_library_async and so on.
await saveToLibraryAsync(localUri)→ NoneSaves a local image or video file to the gallery.
await createAssetAsync(uri)→ AssetSaves the file and returns the new gallery item.
await getPermissionsAsync(writeOnly=False)→ PermissionCurrent permission. writeOnly=True asks about adding only.
await requestPermissionsAsync(writeOnly=False)→ PermissionAsks the user.
Types
Results and options are plain Python dicts with these keys.
Asset
| Key | Type | Description |
|---|---|---|
id | str | Gallery id. |
uri | str | ph://id on iOS, content:// on Android. |
mediaType | "photo" | "video" | |
width / height | int | |
duration | float | Seconds. 0 for photos. |
filename | str |
Permission
| Key | Type | Description |
|---|---|---|
status / granted / canAskAgain / expires | Same as the other modules. | |
accessPrivileges | "all" | "limited" | "none" | limited means the user picked only some photos. |
Examples
Edit and save to the gallery
Pick, resize, then save. Three modules working together.
from pyxpo_image_manipulator import ImageManipulator
from pyxpo_image_picker import ImagePicker
from pyxpo_media_library import MediaLibrary
async def resize_and_save():
res = await ImagePicker.launchImageLibraryAsync()
if res["canceled"]:
return None
small = await ImageManipulator.manipulateAsync(
res["assets"][0]["uri"], [{"resize": {"width": 1080}}],
)
perm = await MediaLibrary.requestPermissionsAsync(writeOnly=True)
if not perm["granted"]:
return None
asset = await MediaLibrary.createAssetAsync(small["uri"])
return asset["id"]Good to know
- Ask with writeOnly=True when you only save. Users trust a smaller request more.
- The module declares the READ_MEDIA_* permissions. Google Play reviews apps that declare them, so be ready to explain the use in the Play Console.