Skip to content

feat: Open Gallery or Specific Image externally (on Android) #103

@stephan-fischer

Description

@stephan-fischer

Description

Combine the functionality to:

  1. Open the default gallery app (if no specific image is provided).
  2. Open a specific image (if a valid URI is provided).

This feature would simplify the API by providing a single method to handle both use cases, making it more intuitive and efficient for developers.

Use Case

  • Open the gallery for general navigation.
  • Open a specific image for viewing or editing, depending on the URI provided.
  • Especially if you display a message - photos saved - with a button "Open Gallery"

Suggested Implementation

Add a method openGallery that accepts an optional URI. If the URI is provided, the function opens the specified image. If no URI is provided, it opens the default gallery app.

Code Example

@PluginMethod
public void openGallery(PluginCall call) {
    try {
        String imageUriString = call.getString("uri"); // Optional URI

        Intent intent;
        if (imageUriString != null && !imageUriString.isEmpty()) {
            // If URI provided, open the specific image
            Uri imageUri = Uri.parse(imageUriString);
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(imageUri, "image/*");
        } else {
            // Otherwise, open the default gallery app
            intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_APP_GALLERY);
        }

        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Open externally
        getActivity().startActivity(intent);

        call.resolve();
    } catch (Exception e) {
        call.reject("Failed to open gallery or image", e);
    }
}

Expected Behavior

  • With URI: Opens the specified image in the system's gallery or media viewer.
  • Without URI: Opens the default gallery app for general navigation.

Platform Support

  • Android: Fully supported with both use cases.
  • iOS: probably not possible

Notes

  • The method should handle errors gracefully when the URI is invalid or no gallery app is available.
  • For Android 7.0+ (API level 24), ensure URI handling is compatible with FileProvider to avoid FileUriExposedException.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions