Skip to content

Commit 2eaa9a3

Browse files
authored
fix: return content uris when possible when selecting from gallery (#902)
1 parent a672c31 commit 2eaa9a3

File tree

1 file changed

+50
-59
lines changed

1 file changed

+50
-59
lines changed

src/android/CameraLauncher.java

+50-59
Original file line numberDiff line numberDiff line change
@@ -728,77 +728,68 @@ private void processResultFromGallery(int destType, Intent intent) {
728728
}
729729
}
730730

731-
String fileLocation = FileHelper.getRealPath(uri, this.cordova);
732-
LOG.d(LOG_TAG, "File location is: " + fileLocation);
733-
734731
String uriString = uri.toString();
735-
String finalLocation = fileLocation != null ? fileLocation : uriString;
736732
String mimeTypeOfGalleryFile = FileHelper.getMimeType(uriString, this.cordova);
737733

738-
if (finalLocation == null) {
739-
this.failPicture("Error retrieving result.");
734+
// If you ask for video or the selected file cannot be processed
735+
// there will be no attempt to resize any returned data.
736+
if (this.mediaType == VIDEO || !isImageMimeTypeProcessable(mimeTypeOfGalleryFile)) {
737+
this.callbackContext.success(uriString);
740738
} else {
741-
// If you ask for video or the selected file cannot be processed
742-
// there will be no attempt to resize any returned data.
743-
if (this.mediaType == VIDEO || !isImageMimeTypeProcessable(mimeTypeOfGalleryFile)) {
744-
this.callbackContext.success(finalLocation);
745-
} else {
746739

747-
// This is a special case to just return the path as no scaling,
748-
// rotating, nor compressing needs to be done
749-
if (this.targetHeight == -1 && this.targetWidth == -1 &&
750-
destType == FILE_URI && !this.correctOrientation &&
751-
getMimetypeForEncodingType().equalsIgnoreCase(mimeTypeOfGalleryFile))
752-
{
753-
this.callbackContext.success(finalLocation);
754-
} else {
755-
Bitmap bitmap = null;
756-
try {
757-
bitmap = getScaledAndRotatedBitmap(uriString);
758-
} catch (IOException e) {
759-
e.printStackTrace();
760-
}
761-
if (bitmap == null) {
762-
LOG.d(LOG_TAG, "I either have a null image path or bitmap");
763-
this.failPicture("Unable to create bitmap!");
764-
return;
765-
}
740+
// This is a special case to just return the path as no scaling,
741+
// rotating, nor compressing needs to be done
742+
if (this.targetHeight == -1 && this.targetWidth == -1 &&
743+
destType == FILE_URI && !this.correctOrientation &&
744+
getMimetypeForEncodingType().equalsIgnoreCase(mimeTypeOfGalleryFile))
745+
{
746+
this.callbackContext.success(uriString);
747+
} else {
748+
Bitmap bitmap = null;
749+
try {
750+
bitmap = getScaledAndRotatedBitmap(uriString);
751+
} catch (IOException e) {
752+
e.printStackTrace();
753+
}
754+
if (bitmap == null) {
755+
LOG.d(LOG_TAG, "I either have a null image path or bitmap");
756+
this.failPicture("Unable to create bitmap!");
757+
return;
758+
}
766759

767-
// If sending base64 image back
768-
if (destType == DATA_URL) {
769-
this.processPicture(bitmap, this.encodingType);
770-
}
760+
// If sending base64 image back
761+
if (destType == DATA_URL) {
762+
this.processPicture(bitmap, this.encodingType);
763+
}
771764

772-
// If sending filename back
773-
else if (destType == FILE_URI) {
774-
// Did we modify the image?
775-
if ( (this.targetHeight > 0 && this.targetWidth > 0) ||
776-
(this.correctOrientation && this.orientationCorrected) ||
777-
!mimeTypeOfGalleryFile.equalsIgnoreCase(getMimetypeForEncodingType()))
778-
{
779-
try {
780-
String modifiedPath = this.outputModifiedBitmap(bitmap, uri, mimeTypeOfGalleryFile);
781-
// The modified image is cached by the app in order to get around this and not have to delete you
782-
// application cache I'm adding the current system time to the end of the file url.
783-
this.callbackContext.success("file://" + modifiedPath + "?" + System.currentTimeMillis());
784-
785-
} catch (Exception e) {
786-
e.printStackTrace();
787-
this.failPicture("Error retrieving image: "+e.getLocalizedMessage());
788-
}
789-
} else {
790-
this.callbackContext.success(finalLocation);
765+
// If sending filename back
766+
else if (destType == FILE_URI) {
767+
// Did we modify the image?
768+
if ( (this.targetHeight > 0 && this.targetWidth > 0) ||
769+
(this.correctOrientation && this.orientationCorrected) ||
770+
!mimeTypeOfGalleryFile.equalsIgnoreCase(getMimetypeForEncodingType()))
771+
{
772+
try {
773+
String modifiedPath = this.outputModifiedBitmap(bitmap, uri, mimeTypeOfGalleryFile);
774+
// The modified image is cached by the app in order to get around this and not have to delete you
775+
// application cache I'm adding the current system time to the end of the file url.
776+
this.callbackContext.success("file://" + modifiedPath + "?" + System.currentTimeMillis());
777+
778+
} catch (Exception e) {
779+
e.printStackTrace();
780+
this.failPicture("Error retrieving image: "+e.getLocalizedMessage());
791781
}
782+
} else {
783+
this.callbackContext.success(uriString);
792784
}
793-
if (bitmap != null) {
794-
bitmap.recycle();
795-
bitmap = null;
796-
}
797-
System.gc();
798785
}
786+
if (bitmap != null) {
787+
bitmap.recycle();
788+
bitmap = null;
789+
}
790+
System.gc();
799791
}
800792
}
801-
802793
}
803794

804795
/**

0 commit comments

Comments
 (0)