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): cleanup camera images if not needed #563

Merged
merged 1 commit into from
Aug 24, 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
Expand Up @@ -316,21 +316,25 @@ private void processEditedImage(PluginCall call, ActivityResult result) {
}

/**
* Save the modified image we've created to a temporary location, so we can
* return a URI to it later
* @param bitmap
* @param contentUri
* Save the modified image on the same path,
* or on a temporary location if it's a content url
* @param uri
* @param is
* @return
* @throws IOException
*/
private Uri saveTemporaryImage(Bitmap bitmap, Uri contentUri, InputStream is) throws IOException {
String filename = Uri.parse(Uri.decode(contentUri.toString())).getLastPathSegment();
if (!filename.contains(".jpg") && !filename.contains(".jpeg")) {
filename += "." + (new java.util.Date()).getTime() + ".jpeg";
private Uri saveImage(Uri uri, InputStream is) throws IOException {
File outFile = null;
if (uri.getScheme().equals("content")) {
String filename = Uri.parse(Uri.decode(uri.toString())).getLastPathSegment();
if (!filename.contains(".jpg") && !filename.contains(".jpeg")) {
filename += "." + (new java.util.Date()).getTime() + ".jpeg";
}
File cacheDir = getContext().getCacheDir();
outFile = new File(cacheDir, filename);
} else {
outFile = new File(uri.getPath());
}
File cacheDir = getContext().getCacheDir();
File outFile = new File(cacheDir, filename);
FileOutputStream fos = new FileOutputStream(outFile);
byte[] buffer = new byte[1024];
int len;
Expand Down Expand Up @@ -360,7 +364,7 @@ private void returnResult(PluginCall call, Bitmap bitmap, Uri u) {
bitmap.compress(Bitmap.CompressFormat.JPEG, settings.getQuality(), bitmapOutputStream);

if (settings.isAllowEditing() && !isEdited) {
editImage(call, bitmap, u, bitmapOutputStream);
editImage(call, u, bitmapOutputStream);
return;
}

Expand All @@ -384,15 +388,26 @@ private void returnResult(PluginCall call, Bitmap bitmap, Uri u) {
} else {
call.reject(INVALID_RESULT_TYPE_ERROR);
}

// Result returned, clear stored paths
// Result returned, clear stored paths and images
if (settings.getResultType() != CameraResultType.URI) {
deleteImageFile();
}
imageFileSavePath = null;
imageFileUri = null;
imageEditedFileSavePath = null;
}

private void deleteImageFile() {
if (imageFileSavePath != null && !settings.isSaveToGallery()) {
File photoFile = new File(imageFileSavePath);
if (photoFile.exists()) {
photoFile.delete();
}
}
}

private void returnFileURI(PluginCall call, ExifWrapper exif, Bitmap bitmap, Uri u, ByteArrayOutputStream bitmapOutputStream) {
Uri newUri = getTempImage(bitmap, u, bitmapOutputStream);
Uri newUri = getTempImage(u, bitmapOutputStream);
exif.copyExif(newUri.getPath());
if (newUri != null) {
JSObject ret = new JSObject();
Expand All @@ -406,12 +421,12 @@ private void returnFileURI(PluginCall call, ExifWrapper exif, Bitmap bitmap, Uri
}
}

private Uri getTempImage(Bitmap bitmap, Uri u, ByteArrayOutputStream bitmapOutputStream) {
private Uri getTempImage(Uri u, ByteArrayOutputStream bitmapOutputStream) {
ByteArrayInputStream bis = null;
Uri newUri = null;
try {
bis = new ByteArrayInputStream(bitmapOutputStream.toByteArray());
newUri = saveTemporaryImage(bitmap, u, bis);
newUri = saveImage(u, bis);
} catch (IOException ex) {} finally {
if (bis != null) {
try {
Expand Down Expand Up @@ -516,9 +531,9 @@ public Map<String, PermissionState> getPermissionStates() {
return permissionStates;
}

private void editImage(PluginCall call, Bitmap bitmap, Uri uri, ByteArrayOutputStream bitmapOutputStream) {
private void editImage(PluginCall call, Uri uri, ByteArrayOutputStream bitmapOutputStream) {
try {
Uri tempImage = getTempImage(bitmap, uri, bitmapOutputStream);
Uri tempImage = getTempImage(uri, bitmapOutputStream);
Intent editIntent = createEditIntent(tempImage);
if (editIntent != null) {
startActivityForResult(call, editIntent, "processEditedImage");
Expand Down