CB-11447 Respect output format when retrieving images from gallery

This commit is contained in:
Vladimir Kotikov 2016-07-26 15:17:44 +03:00
parent 2027d69606
commit fed798e6c7

View File

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