packages

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.

$pip install pyxpo-media-library

Add media-library to modules in your app config, then import it in your code.

pyproject.toml
[tool.pyxpo]
modules = ["media-library"]
python
from pyxpo_media_library import MediaLibrary

This 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

iOSPhotos (PhotoKit)
AndroidMediaStore
Desktop previewNeeds a simulator or device

Quick start

app/index.py
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.

OptionInfo.plist keyDefault
addUsageDescriptionNSPhotoLibraryAddUsageDescriptionSave photos and videos to your photo library.
usageDescriptionNSPhotoLibraryUsageDescriptionAccess the photos and videos in your library.
pyproject.toml
[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.

iOSNSPhotoLibraryAddUsageDescription to add. NSPhotoLibraryUsageDescription for full access.
AndroidAdding: 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)→ None

Saves a local image or video file to the gallery.

await createAssetAsync(uri)→ Asset

Saves the file and returns the new gallery item.

await getPermissionsAsync(writeOnly=False)→ Permission

Current permission. writeOnly=True asks about adding only.

await requestPermissionsAsync(writeOnly=False)→ Permission

Asks the user.

Types

Results and options are plain Python dicts with these keys.

Asset

KeyTypeDescription
idstrGallery id.
uristrph://id on iOS, content:// on Android.
mediaType"photo" | "video"
width / heightint
durationfloatSeconds. 0 for photos.
filenamestr

Permission

KeyTypeDescription
status / granted / canAskAgain / expiresSame 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.

python
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.