Skip to content

Commit f4029b3

Browse files
committed
GH-420 (all) DATA_URL is improperly prefixed
2 parents 5baf535 + 8d42d10 commit f4029b3

File tree

5 files changed

+33
-21
lines changed

5 files changed

+33
-21
lines changed

src/android/CameraLauncher.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public void takePicture(int returnType, int encodingType)
303303
this.imageUri = new CordovaUri(FileProvider.getUriForFile(cordova.getActivity(),
304304
applicationId + ".provider",
305305
photo));
306-
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri.getCorrectUri());
306+
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri.getCorrectUri());
307307
//We can write to this URI, this will hopefully allow us to write files to get to the next step
308308
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
309309

@@ -389,7 +389,7 @@ public void getImage(int srcType, int returnType, int encodingType) {
389389
}
390390
File photo = createCaptureFile(JPEG);
391391
croppedUri = Uri.fromFile(photo);
392-
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, croppedUri);
392+
intent.putExtra(MediaStore.EXTRA_OUTPUT, croppedUri);
393393
} else {
394394
intent.setAction(Intent.ACTION_GET_CONTENT);
395395
intent.addCategory(Intent.CATEGORY_OPENABLE);
@@ -912,14 +912,14 @@ private void writeUncompressedImage(Uri src, Uri dest) throws FileNotFoundExcept
912912
*/
913913
private Uri getUriFromMediaStore() {
914914
ContentValues values = new ContentValues();
915-
values.put(android.provider.MediaStore.Images.Media.MIME_TYPE, JPEG_MIME_TYPE);
915+
values.put(MediaStore.Images.Media.MIME_TYPE, JPEG_MIME_TYPE);
916916
Uri uri;
917917
try {
918-
uri = this.cordova.getActivity().getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
918+
uri = this.cordova.getActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
919919
} catch (RuntimeException e) {
920920
LOG.d(LOG_TAG, "Can't write to external media storage.");
921921
try {
922-
uri = this.cordova.getActivity().getContentResolver().insert(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, values);
922+
uri = this.cordova.getActivity().getContentResolver().insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI, values);
923923
} catch (RuntimeException ex) {
924924
LOG.d(LOG_TAG, "Can't write to internal media storage.");
925925
return null;
@@ -1245,9 +1245,9 @@ private void checkForDuplicateImage(int type) {
12451245
*/
12461246
private Uri whichContentStore() {
12471247
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
1248-
return android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
1248+
return MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
12491249
} else {
1250-
return android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI;
1250+
return MediaStore.Images.Media.INTERNAL_CONTENT_URI;
12511251
}
12521252
}
12531253

@@ -1261,14 +1261,23 @@ public void processPicture(Bitmap bitmap, int encodingType) {
12611261
CompressFormat compressFormat = encodingType == JPEG ?
12621262
CompressFormat.JPEG :
12631263
CompressFormat.PNG;
1264+
String imageMimeType = encodingType == JPEG ?
1265+
JPEG_MIME_TYPE :
1266+
PNG_MIME_TYPE;
12641267

12651268
try {
12661269
if (bitmap.compress(compressFormat, mQuality, jpeg_data)) {
12671270
byte[] code = jpeg_data.toByteArray();
12681271
byte[] output = Base64.encode(code, Base64.NO_WRAP);
12691272
String js_out = new String(output);
1270-
this.callbackContext.success(js_out);
1273+
String base64Prefix = "data:" + imageMimeType + ";base64,";
1274+
String finalJsOut = base64Prefix + js_out;
1275+
1276+
this.callbackContext.success(finalJsOut);
1277+
12711278
js_out = null;
1279+
base64Prefix = null;
1280+
finalJsOut = null;
12721281
output = null;
12731282
code = null;
12741283
}
@@ -1299,7 +1308,7 @@ private void scanForGallery(Uri newImage) {
12991308
public void onMediaScannerConnected() {
13001309
try {
13011310
this.conn.scanFile(this.scanMe.toString(), "image/*");
1302-
} catch (java.lang.IllegalStateException e) {
1311+
} catch (IllegalStateException e) {
13031312
LOG.e(LOG_TAG, "Can't scan file in MediaScanner after taking picture");
13041313
}
13051314

src/browser/CameraProxy.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,8 @@ function takePicture (success, error, opts) {
3636
var reader = new FileReader(); /* eslint no-undef : 0 */
3737
reader.onload = function (readerEvent) {
3838
input.parentNode.removeChild(input);
39-
4039
var imageData = readerEvent.target.result;
41-
42-
return success(imageData.substr(imageData.indexOf(',') + 1));
40+
return success(imageData);
4341
};
4442

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

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

8481
// stop video stream, remove video and button.
8582
// Note that MediaStream.stop() is deprecated as of Chrome 47.

src/ios/CDVCamera.m

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,10 @@ - (void)resultForImage:(CDVPictureOptions*)options info:(NSDictionary*)info comp
493493
image = [self retrieveImage:info options:options];
494494
NSData* data = [self processImage:image info:info options:options];
495495
if (data) {
496-
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:toBase64(data)];
496+
NSString* mimeType = options.encodingType == EncodingTypePNG? @"image/png" : @"image/jpeg";
497+
NSString* finalDataURL = [NSString stringWithFormat:@"%@%@%@%@", @"data:", mimeType, @";base64,", toBase64(data)];
498+
499+
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:finalDataURL];
497500
}
498501
}
499502
break;
@@ -701,7 +704,10 @@ - (void)imagePickerControllerReturnImageResult
701704
break;
702705
case DestinationTypeDataUrl:
703706
{
704-
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:toBase64(self.data)];
707+
NSString* mimeType = self.pickerController.pictureOptions.encodingType == EncodingTypePNG ? @"image/png" : @"image/jpeg";
708+
NSString* finalDataURL = [NSString stringWithFormat:@"%@%@%@%@", @"data:", mimeType, @";base64,", toBase64(self.data)];
709+
710+
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:finalDataURL];
705711
}
706712
break;
707713
case DestinationTypeNativeUri:

src/osx/CDVCamera.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,10 @@ - (void)returnImage:(NSImage *)image command:(CDVInvokedUrlCommand *)command opt
180180
NSData *processedImageData = [self processImage:image options:pictureOptions];
181181

182182
if (pictureOptions.destinationType == DestinationTypeDataUrl) {
183-
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[processedImageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]];
183+
NSString* mimeType = pictureOptions.encodingType == EncodingTypeJPEG? @"image/jpeg" : @"image/png";
184+
NSString* finalDataURL = [NSString stringWithFormat:@"%@%@%@%@", @"data:", mimeType, @";base64,", [processedImageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]];
185+
186+
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:finalDataURL];
184187
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
185188
} else {
186189
NSString *tempFilePath = [self uniqueImageName:pictureOptions];
@@ -255,4 +258,4 @@ - (NSString *)uniqueImageName:(CDVPictureOptions *)pictureOptions {
255258
return uniqueFileName;
256259
}
257260

258-
@end
261+
@end

src/windows/CameraProxy.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,7 @@ function resizeImageBase64 (successCallback, errorCallback, file, targetWidth, t
153153
// The resized file ready for upload
154154
var finalFile = canvas.toDataURL(file.contentType);
155155

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

0 commit comments

Comments
 (0)