Class MediaLibrary

java.lang.Object
com.codename1.media.MediaLibrary

public final class MediaLibrary extends Object

Saves images and videos to the device photo gallery: Photos on iOS and the shared media collection on Android.

Referencing this class makes the build declare what saving needs: the NSPhotoLibraryAddUsageDescription Info.plist string on iOS, and on Android WRITE_EXTERNAL_STORAGE for Android 9 and older only. Later Android versions need no permission. The iOS prompt text can be replaced with the ios.NSPhotoLibraryAddUsageDescription build hint.

The callback is called on the EDT with true when the system accepted the file. In the simulator the file is copied into a gallery folder in the app home directory.

String path = FileSystemStorage.getInstance().getAppHomePath() + "photo.jpg";
MediaLibrary.saveImage(path, "My App", new SuccessCallback<Boolean>() {
    public void onSucess(Boolean saved) {
        Log.p("Saved to the gallery: " + saved);
    }
});
  • Method Details

    • setImplementation

      public static void setImplementation(com.codename1.impl.CodenameOneImplementation impl)

      Invoked internally from Display, this method is for internal use only

      Parameters
      • impl: implementation instance
    • isSupported

      public static boolean isSupported()

      True when this platform can save to a gallery.

      Returns

      true when saving is supported

    • saveImage

      public static void saveImage(String path, String album, SuccessCallback<Boolean> callback)

      Saves an image file to the gallery.

      Parameters
      • path: the FileSystemStorage path of a JPEG or PNG file

      • album: an album name, or null for the default location. Android uses it as a folder under Pictures; iOS ignores it.

      • callback: receives true on success; may be null

    • saveVideo

      public static void saveVideo(String path, String album, SuccessCallback<Boolean> callback)

      Saves a video file to the gallery.

      Parameters
      • path: the FileSystemStorage path of an MP4 or MOV file

      • album: an album name, or null for the default location. Android uses it as a folder under Movies; iOS ignores it.

      • callback: receives true on success; may be null