diff --git a/camera/android/src/main/java/com/capacitorjs/plugins/camera/CameraPlugin.java b/camera/android/src/main/java/com/capacitorjs/plugins/camera/CameraPlugin.java index cbbfd9f0f..27b786efd 100644 --- a/camera/android/src/main/java/com/capacitorjs/plugins/camera/CameraPlugin.java +++ b/camera/android/src/main/java/com/capacitorjs/plugins/camera/CameraPlugin.java @@ -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; @@ -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; } @@ -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(); @@ -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 { @@ -516,9 +531,9 @@ public Map 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");