diff --git a/src/android/CameraLauncher.java b/src/android/CameraLauncher.java index 9db9911..060103b 100644 --- a/src/android/CameraLauncher.java +++ b/src/android/CameraLauncher.java @@ -365,7 +365,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); } - File photo = createCaptureFile(encodingType); + File photo = createCaptureFile(JPEG); croppedUri = Uri.fromFile(photo); intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, croppedUri); } else { @@ -592,15 +592,29 @@ private void refreshGallery(Uri contentUri) this.cordova.getActivity().sendBroadcast(mediaScanIntent); } + /** + * Converts output image format int value to string value of mime type. + * @param outputFormat int Output format of camera API. + * Must be value of either JPEG or PNG constant + * @return String String value of mime type or empty string if mime type is not supported + */ + private String getMimetypeForFormat(int outputFormat) { + if (outputFormat == PNG) return "image/png"; + if (outputFormat == JPEG) return "image/jpeg"; + return ""; + } -private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException { + private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException { // Some content: URIs do not map to file paths (e.g. picasa). String realPath = FileHelper.getRealPath(uri, this.cordova); // Get filename from uri String fileName = realPath != null ? - realPath.substring(realPath.lastIndexOf('/') + 1) : - "modified." + (this.encodingType == JPEG ? "jpg" : "png"); + realPath.substring(realPath.lastIndexOf('/') + 1, realPath.lastIndexOf(".") + 1) : + "modified."; + + // Append filename extension based on output encoding type + fileName += (this.encodingType == JPEG ? "jpg" : "png"); String modifiedPath = getTempDirectoryPath() + "/" + fileName; @@ -660,15 +674,18 @@ private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException { this.callbackContext.success(fileLocation); } else { + String uriString = uri.toString(); + // Get the path to the image. Makes loading so much easier. + String mimeType = FileHelper.getMimeType(uriString, this.cordova); + // This is a special case to just return the path as no scaling, // rotating, nor compressing needs to be done if (this.targetHeight == -1 && this.targetWidth == -1 && - (destType == FILE_URI || destType == NATIVE_URI) && !this.correctOrientation) { - this.callbackContext.success(uri.toString()); + (destType == FILE_URI || destType == NATIVE_URI) && !this.correctOrientation && + mimeType.equalsIgnoreCase(getMimetypeForFormat(encodingType))) + { + this.callbackContext.success(uriString); } else { - String uriString = uri.toString(); - // Get the path to the image. Makes loading so much easier. - String mimeType = FileHelper.getMimeType(uriString, this.cordova); // If we don't have a valid image so quit. if (!("image/jpeg".equalsIgnoreCase(mimeType) || "image/png".equalsIgnoreCase(mimeType))) { Log.d(LOG_TAG, "I either have a null image path or bitmap"); @@ -710,7 +727,9 @@ private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException { else if (destType == FILE_URI || destType == NATIVE_URI) { // Did we modify the image? if ( (this.targetHeight > 0 && this.targetWidth > 0) || - (this.correctOrientation && this.orientationCorrected) ) { + (this.correctOrientation && this.orientationCorrected) || + !mimeType.equalsIgnoreCase(getMimetypeForFormat(encodingType))) + { try { String modifiedPath = this.ouputModifiedBitmap(bitmap, uri); // The modified image is cached by the app in order to get around this and not have to delete you