Skip to content

Commit

Permalink
GH-420 (all) DATA_URL is improperly prefixed
Browse files Browse the repository at this point in the history
  • Loading branch information
hazems committed Mar 8, 2019
1 parent 81b878d commit fd45329
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 21 deletions.
27 changes: 18 additions & 9 deletions src/android/CameraLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public void takePicture(int returnType, int encodingType)
this.imageUri = new CordovaUri(FileProvider.getUriForFile(cordova.getActivity(),
applicationId + ".provider",
photo));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri.getCorrectUri());
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri.getCorrectUri());
//We can write to this URI, this will hopefully allow us to write files to get to the next step
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

Expand Down Expand Up @@ -389,7 +389,7 @@ public void getImage(int srcType, int returnType, int encodingType) {
}
File photo = createCaptureFile(JPEG);
croppedUri = Uri.fromFile(photo);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, croppedUri);
intent.putExtra(MediaStore.EXTRA_OUTPUT, croppedUri);
} else {
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
Expand Down Expand Up @@ -912,14 +912,14 @@ private void writeUncompressedImage(Uri src, Uri dest) throws FileNotFoundExcept
*/
private Uri getUriFromMediaStore() {
ContentValues values = new ContentValues();
values.put(android.provider.MediaStore.Images.Media.MIME_TYPE, JPEG_MIME_TYPE);
values.put(MediaStore.Images.Media.MIME_TYPE, JPEG_MIME_TYPE);
Uri uri;
try {
uri = this.cordova.getActivity().getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
uri = this.cordova.getActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} catch (RuntimeException e) {
LOG.d(LOG_TAG, "Can't write to external media storage.");
try {
uri = this.cordova.getActivity().getContentResolver().insert(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, values);
uri = this.cordova.getActivity().getContentResolver().insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI, values);
} catch (RuntimeException ex) {
LOG.d(LOG_TAG, "Can't write to internal media storage.");
return null;
Expand Down Expand Up @@ -1245,9 +1245,9 @@ private void checkForDuplicateImage(int type) {
*/
private Uri whichContentStore() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
return MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else {
return android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI;
return MediaStore.Images.Media.INTERNAL_CONTENT_URI;
}
}

Expand All @@ -1261,14 +1261,23 @@ public void processPicture(Bitmap bitmap, int encodingType) {
CompressFormat compressFormat = encodingType == JPEG ?
CompressFormat.JPEG :
CompressFormat.PNG;
String imageMimeType = encodingType == JPEG ?
JPEG_MIME_TYPE :
PNG_MIME_TYPE;

try {
if (bitmap.compress(compressFormat, mQuality, jpeg_data)) {
byte[] code = jpeg_data.toByteArray();
byte[] output = Base64.encode(code, Base64.NO_WRAP);
String js_out = new String(output);
this.callbackContext.success(js_out);
String base64Prefix = "data:" + imageMimeType + ";base64,";
String final_js_out = base64Prefix + js_out;

this.callbackContext.success(final_js_out);

js_out = null;
base64Prefix = null;
final_js_out = null;
output = null;
code = null;
}
Expand Down Expand Up @@ -1299,7 +1308,7 @@ private void scanForGallery(Uri newImage) {
public void onMediaScannerConnected() {
try {
this.conn.scanFile(this.scanMe.toString(), "image/*");
} catch (java.lang.IllegalStateException e) {
} catch (IllegalStateException e) {
LOG.e(LOG_TAG, "Can't scan file in MediaScanner after taking picture");
}

Expand Down
5 changes: 1 addition & 4 deletions src/browser/CameraProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ function takePicture (success, error, opts) {
var reader = new FileReader(); /* eslint no-undef : 0 */
reader.onload = function (readerEvent) {
input.parentNode.removeChild(input);

var imageData = readerEvent.target.result;

return success(imageData.substr(imageData.indexOf(',') + 1));
return success(imageData);
};

reader.readAsDataURL(inputEvent.target.files[0]);
Expand Down Expand Up @@ -79,7 +77,6 @@ function capture (success, errorCallback, opts) {

// convert image stored in canvas to base64 encoded image
var imageData = canvas.toDataURL('image/png');
imageData = imageData.replace('data:image/png;base64,', '');

// stop video stream, remove video and button.
// Note that MediaStream.stop() is deprecated as of Chrome 47.
Expand Down
10 changes: 8 additions & 2 deletions src/ios/CDVCamera.m
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,10 @@ - (void)resultForImage:(CDVPictureOptions*)options info:(NSDictionary*)info comp
image = [self retrieveImage:info options:options];
NSData* data = [self processImage:image info:info options:options];
if (data) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:toBase64(data)];
NSString* mimeType = options.encodingType == EncodingTypePNG? @"image/png" : @"image/jpeg";
NSString* finalDataURL = [NSString stringWithFormat:@"%@%@%@%@", @"data:", mimeType, @";base64,", toBase64(data)];

result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:finalDataURL];
}
}
break;
Expand Down Expand Up @@ -701,7 +704,10 @@ - (void)imagePickerControllerReturnImageResult
break;
case DestinationTypeDataUrl:
{
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:toBase64(self.data)];
NSString* mimeType = self.pickerController.pictureOptions.encodingType == EncodingTypePNG ? @"image/png" : @"image/jpeg";
NSString* finalDataURL = [NSString stringWithFormat:@"%@%@%@%@", @"data:", mimeType, @";base64,", toBase64(self.data)];

result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:finalDataURL];
}
break;
case DestinationTypeNativeUri:
Expand Down
7 changes: 5 additions & 2 deletions src/osx/CDVCamera.m
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ - (void)returnImage:(NSImage *)image command:(CDVInvokedUrlCommand *)command opt
NSData *processedImageData = [self processImage:image options:pictureOptions];

if (pictureOptions.destinationType == DestinationTypeDataUrl) {
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[processedImageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]];
NSString* mimeType = pictureOptions.encodingType == EncodingTypeJPEG? @"image/jpeg" : @"image/png";
NSString* finalDataURL = [NSString stringWithFormat:@"%@%@%@%@", @"data:", mimeType, @";base64,", [processedImageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]];

CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:finalDataURL];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
} else {
NSString *tempFilePath = [self uniqueImageName:pictureOptions];
Expand Down Expand Up @@ -255,4 +258,4 @@ - (NSString *)uniqueImageName:(CDVPictureOptions *)pictureOptions {
return uniqueFileName;
}

@end
@end
5 changes: 1 addition & 4 deletions src/windows/CameraProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,7 @@ function resizeImageBase64 (successCallback, errorCallback, file, targetWidth, t
// The resized file ready for upload
var finalFile = canvas.toDataURL(file.contentType);

// Remove the prefix such as "data:" + contentType + ";base64," , in order to meet the Cordova API.
var arr = finalFile.split(',');
var newStr = finalFile.substr(arr[0].length + 1);
successCallback(newStr);
successCallback(finalFile);
};
}, function (err) { errorCallback(err); });
}
Expand Down

0 comments on commit fd45329

Please sign in to comment.