Gal

repository·main·Indexed 42 days ago

https://github.com/natsuk4ze/gal

A Dart 3 plugin for Flutter applications to save images and videos directly to the device's photo gallery or specific albums across multiple platforms. It provides the Gal class for media operations and GalException for structured error handling, including categories like accessDenied, notEnoughSpace, and notSupportedFormat.

Tokens
1.9K
Snippets
9
Records
14
Agent score
51%

What's inside Gal

  1. Customize iOS launch screen assets

    main

    To change the launch screen image for your iOS application, you can use one of two methods:

    1. Direct File Replacement: Replace the existing image files located in the example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory with your own assets.
    2. Xcode Interface:
      • Open your Flutter project's iOS workspace using open ios/Runner.xcworkspace.
      • In the Xcode Project Navigator, navigate to Runner/Assets.xcassets.
      • Drag and drop your desired images into the asset catalog.
    open ios/Runner.xcworkspace
  2. Configure iOS permissions

    main

    Add the following keys to your ios/Runner/Info.plist file to allow the app to access and save to the photo library.

    • NSPhotoLibraryAddUsageDescription: Required for saving images/videos.
    • NSPhotoLibraryUsageDescription: Required for iOS versions older than 14, or when saving specifically to an album.
  3. Configure Android permissions

    main

    Update android/app/src/main/AndroidManifest.xml with the following requirements:

    1. For API levels <= 29, add the storage write permission: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="29" />
    2. For saving to albums in API 29, add android:requestLegacyExternalStorage="true" to the <application> tag.

    Note: Android emulators with API < 29 require an SD card setup; real devices do not.

  4. Save media downloaded from the internet

    main

    To save media from a URL, first download the file to a temporary directory (e.g., using dio) and then pass the local path to Gal.

    // Requires 'dio' package
    // Download Image
    final imagePath = '${Directory.systemTemp.path}/image.jpg';
    await Dio().download('$url', imagePath);
    await Gal.putImage(imagePath);
    
    // Download Video
    final videoPath = '${Directory.systemTemp.path}/video.mp4';
    await Dio().download('$url', videoPath);
    await Gal.putVideo(videoPath);
  5. Save media captured from camera

    main

    Integrate with image_picker or camera packages to capture media and immediately save it to the gallery.

    // Using image_picker
    // Requires 'image_picker' package
    final image = await ImagePicker.pickImage(source: ImageSource.camera);
    await Gal.putImage(image.path);
    
    // Using camera
    // Requires 'camera' package
    // ... capture video logic
    final video = await controller.stopVideoRecording();
    await Gal.putVideo(video.path);
  6. Handle GalException errors

    main

    When performing gallery operations, wrap calls in a try-catch block to handle GalException. The exception provides a type which can be used to determine the cause of the failure.

    try {
      await Gal.putImage('$filePath');
    } on GalException catch (e) {
      log(e.type.message);
    }
    
    // GalExceptionType includes:
    // - accessDenied
    // - notEnoughSpace
    // - notSupportedFormat
    // - unexpected
  7. Handle gallery permissions

    main

    Before saving media, you should check and request access to the photo library. You can also specify if you are requesting access specifically for album operations.

    // Check Access Permission
    final hasAccess = await Gal.hasAccess();
    
    // Request Access Permission
    await Gal.requestAccess();
    
    // ... for saving to album
    final hasAccess = await Gal.hasAccess(toAlbum: true);
    await Gal.requestAccess(toAlbum: true);
  8. Save images and videos to the gallery

    main

    Use the Gal class to save media from local file paths, bytes, or specific albums.

    // Save Image (Supports two ways)
    await Gal.putImage('$filePath');
    await Gal.putImageBytes('$uint8List');
    
    // Save Video
    await Gal.putVideo('$filePath');
    
    // Save to Album
    await Gal.putImage('$filePath', album: '$album');