Skip to content

Commit

Permalink
fix(camera): return original image if editing is cancelled (#566)
Browse files Browse the repository at this point in the history
* fix(camera): return original image if editing is cancelled

* fix(camera): Fix allowEdit: true when picking from photos
  • Loading branch information
mlynch authored Aug 26, 2021
1 parent a2e4f43 commit 4786841
Showing 1 changed file with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.capacitorjs.plugins.camera;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
Expand Down Expand Up @@ -75,6 +76,7 @@ public class CameraPlugin extends Plugin {
private String imageFileSavePath;
private String imageEditedFileSavePath;
private Uri imageFileUri;
private Uri imagePickedContentUri;
private boolean isEdited = false;

private CameraSettings settings = new CameraSettings();
Expand Down Expand Up @@ -282,18 +284,24 @@ public void processPickedImage(PluginCall call, ActivityResult result) {

Uri u = data.getData();

imagePickedContentUri = u;

processPickedImage(u, call);
}

private void processPickedImage(Uri imageUri, PluginCall call) {
InputStream imageStream = null;

try {
imageStream = getContext().getContentResolver().openInputStream(u);
imageStream = getContext().getContentResolver().openInputStream(imageUri);
Bitmap bitmap = BitmapFactory.decodeStream(imageStream);

if (bitmap == null) {
call.reject("Unable to process bitmap");
return;
}

returnResult(call, bitmap, u);
returnResult(call, bitmap, imageUri);
} catch (OutOfMemoryError err) {
call.reject("Out of memory");
} catch (FileNotFoundException ex) {
Expand All @@ -312,7 +320,18 @@ public void processPickedImage(PluginCall call, ActivityResult result) {
@ActivityCallback
private void processEditedImage(PluginCall call, ActivityResult result) {
isEdited = true;
processPickedImage(call, result);

if (result.getResultCode() == Activity.RESULT_CANCELED) {
// User cancelled the edit operation, if this file was picked from photos,
// process the original picked image, otherwise process it as a camera photo
if (imagePickedContentUri != null) {
processPickedImage(imagePickedContentUri, call);
} else {
processCameraImage(call, result);
}
} else {
processPickedImage(call, result);
}
}

/**
Expand Down Expand Up @@ -394,6 +413,7 @@ private void returnResult(PluginCall call, Bitmap bitmap, Uri u) {
}
imageFileSavePath = null;
imageFileUri = null;
imagePickedContentUri = null;
imageEditedFileSavePath = null;
}

Expand Down

0 comments on commit 4786841

Please sign in to comment.