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:
- Open any Capacitor app with
@capacitor/camera installed.
- Go to Android Developer Options and turn ON "Don't keep activities".
- Trigger the
Camera.pickImages() or Camera.getPhoto() method.
- Select a photo from the gallery.
- 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();
}
Bug Report
Plugin(s)
@capacitor/camerav8.2.0Capacitor 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 theActivityResultLauncher(pickMediaorpickMultipleMedia).Because the activity was recreated, the launcher reference is
null, resulting in a fatalNullPointerExceptioninsideLegacyCameraFlow.javaat 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
ActivityResultLauncheris 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:
@capacitor/camerainstalled.Camera.pickImages()orCamera.getPhoto()method.NullPointerExceptionupon returning.Other Technical Details
android/src/main/java/com/capacitorjs/plugins/camera/LegacyCameraFlow.java.Additional Context
Stack Trace: