Skip to content

bug: [Camera] NullPointerException on ActivityResultLauncher.unregister() in LegacyCameraFlow (Android) #2537

@webjoker-india

Description

@webjoker-india

Bug Report

Plugin(s)

@capacitor/camera v8.2.0

Capacitor Version

💊 Capacitor Doctor 💊

Latest Dependencies:

@capacitor/cli: 8.3.4
@capacitor/core: 8.3.4
@capacitor/android: 8.3.4
@capacitor/ios: 8.3.4

Installed Dependencies:

@capacitor/ios: not installed
@capacitor/cli: 8.0.1
@capacitor/core: 8.0.1
@capacitor/android: 8.0.1

[success] Android looking great! 👌

Platform(s)

Android (Observed on Android 15)

Current Behavior

When a user opens the photo gallery to pick an image, the Android OS may suspend or destroy the underlying app activity in the background to free up memory (especially on devices using the new system-level Photo Picker). When the user selects a photo and returns to the app, the plugin attempts to clean up by calling .unregister() on the ActivityResultLauncher (pickMedia or pickMultipleMedia).

Because the activity was recreated, the launcher reference is null, resulting in a fatal NullPointerException inside LegacyCameraFlow.java at line 362, which crashes the entire app.

Expected Behavior

The app should resume gracefully and process the selected image without crashing. The plugin should check if the ActivityResultLauncher is null before attempting to unregister it.

Code Reproduction

This is a standard Android lifecycle issue and is difficult to reproduce under normal testing conditions. To easily reproduce it:

  1. Open any Capacitor app with @capacitor/camera installed.
  2. Go to Android Developer Options and turn ON "Don't keep activities".
  3. Trigger the Camera.pickImages() or Camera.getPhoto() method.
  4. Select a photo from the gallery.
  5. The app will immediately crash with a NullPointerException upon returning.

Other Technical Details

  • Bug is located in android/src/main/java/com/capacitorjs/plugins/camera/LegacyCameraFlow.java.
  • Happens on Capacitor Camera v8.2.0.

Additional Context

Stack Trace:

Fatal Exception: java.lang.RuntimeException
java.lang.reflect.InvocationTargetException
com.getcapacitor.Bridge.lambda$callPluginMethod$0 (Bridge.java:850)
android.os.HandlerThread.run (HandlerThread.java:85)

Caused by java.lang.NullPointerException
Attempt to invoke virtual method 'void androidx.activity.result.ActivityResultLauncher.unregister()' on a null object reference
com.capacitorjs.plugins.camera.LegacyCameraFlow.lambda$openPhotos$4 (LegacyCameraFlow.java:362)
com.capacitorjs.plugins.camera.LegacyCameraFlow.$r8$lambda$G8XQNzjKitqrlvcAK_ocqIM0g5g
com.capacitorjs.plugins.camera.LegacyCameraFlow$$ExternalSyntheticLambda3.onActivityResult (D8$$SyntheticClass)

### Proposed Fix
In `android/src/main/java/com/capacitorjs/plugins/camera/LegacyCameraFlow.java`, inside the `openPhotos` method, add a null check around the `.unregister()` calls (around line 353 and 367):

```java
// For pickMultipleMedia
if (pickMultipleMedia != null) {
    pickMultipleMedia.unregister();
}

// For pickMedia
if (pickMedia != null) {
    pickMedia.unregister();
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions