Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
stephan-fischer opened this issue Dec 13, 2024 · 0 comments
Open

Comments

@stephan-fischer
Copy link
Contributor

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant