ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web ⚡️
https://capacitorjs.com
MIT License
12.32k stars 1.01k forks source link

bug: Camera API crashing on edit photo in any edit app #2837

Closed makinox closed 3 years ago

makinox commented 4 years ago

Bug Report

Capacitor Version

npx cap doctor output: @capacitor/android 2.0.1 @capacitor/ios 2.0.1 @capacitor/cli 2.0.1 @capacitor/core 2.0.1

Affected Platform(s)

Current Behavior

Expected Behavior

Return a photo. (Base64, uri, dataUri)

Sample Code or Sample Application Repo

camera.ts: getPicture()

try {
  const image = await Plugins.Camera.getPhoto({
    quality: 100,
    allowEditing: true,
    resultType: CameraResultType.DataUrl,
    source: CameraSource.Camera,
//  source: CameraSource.Photos,
  });
  console.log(image);
} catch (error) {
  console.log(error);
}

app.components.ts: this.platform.ready().then(() => {})

Plugins.App.addListener('appRestoredResult', (data: any) => {
  console.log('Restored state:', data);
});

Reproduction Steps

Other Technical Details

npm --version output: 6.14.4

node --version output: v10.16.3

jcesarmobile commented 4 years ago

Can you provide a sample app?

makinox commented 4 years ago

I can send you an apk (?

makinox commented 4 years ago

Android debug output:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.jesusbossa.factiffy, PID: 6658
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=9005, result=-1, data=Intent { (has extras) }} to activity {com.jesusbossa.factiffy/com.jesusbossa.factiffy.MainActivity}: java.lang.NullPointerException: uri
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5097)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:5138)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2147)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:7814)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068)
     Caused by: java.lang.NullPointerException: uri
        at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:133)
        at android.content.ContentResolver.openInputStream(ContentResolver.java:1177)
        at com.getcapacitor.plugin.Camera.processPickedImage(Camera.java:256)
        at com.getcapacitor.plugin.Camera.handleOnActivityResult(Camera.java:486)
        at com.getcapacitor.Bridge.onActivityResult(Bridge.java:770)
        at com.getcapacitor.BridgeActivity.onActivityResult(BridgeActivity.java:212)
        at android.app.Activity.dispatchActivityResult(Activity.java:8292)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5090)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:5138) 
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2147) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:237) 
        at android.app.ActivityThread.main(ActivityThread.java:7814) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068)
jcesarmobile commented 4 years ago

I mean code, not an apk. What I need is an easy way of reproducing and verify that my changes fix it.

makinox commented 4 years ago

Ok, i created this new app with the bug: https://github.com/makinox/cam-edit

jcesarmobile commented 4 years ago

I can't reproduce, tested with "Photos" and "Gallery" apps ("Gallery" appears as "Editor")

What's your device model and Android version?

makinox commented 4 years ago

Yes is very weird, in some devices works and in others it doesn't: Samsung galaxy s8+: (manually install, android9) : works Samsung galaxy a70: (android studio plug install, android9): crash

makinox commented 4 years ago

PD: Remember edit the photo, crop, add some filter, i think the error is caused because the app return a different type of image and that file doesn't have the element 'uri', throwing the error:

java.lang.NullPointerException: uri
jcesarmobile commented 4 years ago

maybe the problem is not the app you use to edit but the image you pick, where is it located?

makinox commented 4 years ago

Maybe the camera or the gallery. I think the issue is with the edited image the the editor export. Try to edit an image with a random editor like Snapseed or Lightroom.

sean118 commented 4 years ago

Maybe it could be related to this: https://github.com/ionic-team/capacitor/issues/2171 Possibly it works when on Android a "Default App" has been set, and throws this error when not? That may be what makes these hard to reproduce.

jcesarmobile commented 4 years ago

no, I don't set a default app and it works on allmy devices, but some apps don't return what we expect and can cause problems, the thing is the OP says it happens with any app, including google photos and gallery, those 2 work fine on all my devices (7 different devices from different brands)

ansonylopez commented 3 years ago

Hi Everyone, I've the same problem, but I can reproduce it, using a Huawei P30 with this plugin import { Camera, CameraOptions } from '@ionic-native/camera/ngx';

This is my code

`import { Injectable } from '@angular/core'; import { Camera, CameraOptions } from '@ionic-native/camera/ngx'; import { AlertService } from '../alert/alert.service'; import { AppRestoredResult, AppState, Plugins } from '@capacitor/core';

const { App } = Plugins;

@Injectable({ providedIn: 'root' }) export class CameraService {

imgToSave: File; profileImg: string;

constructor(private camera: Camera) {

App.addListener('appRestoredResult', data => console.log(JSON.stringify(data)));

App.addListener('appStateChange', (state: AppState) => {
  console.log('App state changed. Is active?', state.isActive);
});

}

getImage() { const options: CameraOptions = { quality: 100, destinationType: this.camera.DestinationType.DATA_URL, allowEdit: true, sourceType: this.camera.PictureSourceType.PHOTOLIBRARY, saveToPhotoAlbum:false, correctOrientation: true };

return this.camera.getPicture(options).then((imageData) => {

      const imgBase64 = 'data:image/jpeg;base64,' + imageData;

      this.urltoFile(imgBase64, 'profile.jpeg', 'image/jpeg').then((img) => {
        this.imgToSave = img;
      })
      return this.profileImg = imgBase64;
}, (err)=> {
  console.log("ERROR",err);
});

}

urltoFile(url, filename, mimeType){ return (fetch(url) .then(function(res){return res.arrayBuffer();}) .then(function(buf){return new File([buf], filename,{type:mimeType});}) ); } } ` I cannot see the log of appRestoredResult listener. I hope that this will helpful

vaReliy commented 3 years ago

I have similar problem in Capacitor v3 (3.0.0-beta.3) too.

While using @capacitor/camera plugin for loading photo/images the app has been crashing. App crashing accures if used parameter allowEditing: true, after click to "Done" button in android app. The web app works fine. I don't have the opportunity to test iOS app

npx cap doctor
Installed Dependencies:

  @capacitor/ios: not installed
  @capacitor/cli: 3.0.0-beta.3
  @capacitor/core: 3.0.0-beta.3
  @capacitor/android: 3.0.0-beta.3

Tested on emulator (Pixel 4 API 30)

The demo project with this issue: https://github.com/vaReliy/cap3-camera-ex

I will be grateful if you have time to check and answer.

The error message (full message):

02/26 20:25:40: Launching 'app' on Pixel 4 API 30.
    ....
    V/Capacitor/Plugin: To native (Capacitor plugin): callbackId: 20065059, pluginId: Camera, methodName: getPhoto
    V/Capacitor: callback: 20065059, pluginId: Camera, methodName: getPhoto, methodData: {{'{'}}"quality":90,"allowEditing":true,"resultType":"uri","saveToGallery":true{{'}'}}
    D/Capacitor: App paused
    D/Capacitor: App stopped
    D/Capacitor: Saving instance state!
    D/Capacitor: Unable to find a Capacitor plugin to handle requestCode, trying Cordova plugins 167584407
    D/Capacitor: App restarted
    D/Capacitor: App started
    D/Capacitor: App resumed
    D/Capacitor: App paused
    D/Capacitor: App stopped
    D/Capacitor: Saving instance state!
    W/System: A resource failed to call release. 
        A resource failed to call release. 
    D/Capacitor: Unable to find a Capacitor plugin to handle requestCode, trying Cordova plugins 478330788
    D/Capacitor: App restarted
    D/Capacitor: App started
    W/System.err: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
    W/System.err:     at com.getcapacitor.Plugin.triggerActivityCallback(Plugin.java:165)
            at com.getcapacitor.Plugin.lambda$initializeActivityLaunchers$0$Plugin(Plugin.java:120)
    W/System.err:     at com.getcapacitor.-$$Lambda$Plugin$9tit0PZrRMA5UA4Z_xqI4yDk4JY.onActivityResult(Unknown Source:6)
    W/System.err:     at androidx.activity.result.ActivityResultRegistry$1.onStateChanged(ActivityResultRegistry.java:145)
    W/System.err:     at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)
    W/System.err:     at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:265)
    W/System.err:     at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:307)
            at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:148)
    W/System.err:     at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:134)
    W/System.err:     at androidx.lifecycle.ReportFragment.dispatch(ReportFragment.java:68)
    W/System.err:     at androidx.lifecycle.ReportFragment$LifecycleCallbacks.onActivityPostStarted(ReportFragment.java:187)
            at android.app.Activity.dispatchActivityPostStarted(Activity.java:1362)
    W/System.err:     at android.app.Activity.performStart(Activity.java:8061)
    W/System.err:     at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3475)
    W/System.err:     at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
            at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
    W/System.err:     at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
    W/System.err:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
    W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:223)
    W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7656)
            at java.lang.reflect.Method.invoke(Native Method)
    W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
    W/System.err: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.getcapacitor.PluginCall.reject(java.lang.String)' on a null object reference
            at com.capacitorjs.plugins.camera.CameraPlugin.processPickedImage(CameraPlugin.java:281)
    W/System.err:     at com.capacitorjs.plugins.camera.CameraPlugin.processEditedImage(CameraPlugin.java:317)
          ... 26 more
    D/AndroidRuntime: Shutting down VM
    E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.app, PID: 5478
        java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.getcapacitor.PluginCall.isReleased()' on a null object reference
            at com.getcapacitor.Plugin.triggerActivityCallback(Plugin.java:170)
            at com.getcapacitor.Plugin.lambda$initializeActivityLaunchers$0$Plugin(Plugin.java:120)
            at com.getcapacitor.-$$Lambda$Plugin$9tit0PZrRMA5UA4Z_xqI4yDk4JY.onActivityResult(Unknown Source:6)
            at androidx.activity.result.ActivityResultRegistry$1.onStateChanged(ActivityResultRegistry.java:145)
            at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)
            at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:265)
            at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:307)
            at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:148)
            at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:134)
            at androidx.lifecycle.ReportFragment.dispatch(ReportFragment.java:68)
            at androidx.lifecycle.ReportFragment$LifecycleCallbacks.onActivityPostStarted(ReportFragment.java:187)
            at android.app.Activity.dispatchActivityPostStarted(Activity.java:1362)
            at android.app.Activity.performStart(Activity.java:8061)
            at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3475)
            at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
            at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
            at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
            at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:223)
            at android.app.ActivityThread.main(ActivityThread.java:7656)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
    I/Process: Sending signal. PID: 5478 SIG: 9
    Disconnected from the target VM, address: 'localhost:40633', transport: 'socket'
marouane-souadi commented 3 years ago

for me, it was due to the use of androidx.lifecycle import in my project, which is not compatible with capacitor. capacitor use the old android libraries. when i removed the use of that library, things returned working well

jcesarmobile commented 3 years ago

camera is now a separate plugin in a separate repository, so closing in favor of https://github.com/ionic-team/capacitor-plugins/issues/307

ionitron-bot[bot] commented 2 years ago

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.