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

fix(camera): return original image if editing is cancelled #566

Merged
merged 2 commits into from
Aug 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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