Skip to content

Commit

Permalink
fix(camera): cleanup camera images if not needed (#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed Aug 24, 2021
1 parent a70414e commit a2e4f43
Showing 1 changed file with 33 additions and 18 deletions.
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

0 comments on commit a2e4f43

Please sign in to comment.