This commit is contained in:
ldeluca 2014-05-20 13:54:43 -04:00
commit 2eb71f648f
3 changed files with 38 additions and 65 deletions

View File

@ -92,13 +92,6 @@ scenario, the image may not appear when the cordova activity is restored.
### Android Quirks ### Android Quirks
*Android 4.4 only*: Android 4.4 introduced a new [Storage Access Framework](https://developer.android.com/guide/topics/providers/document-provider.html) that makes it
easier for users to browse and open documents across all of their preferred document storage providers.
Cordova has not yet been fully integrated with this new Storage Access Framework. Because of this, the `getPicture()`
method will not correctly return pictures when the user selects from the "Recent", "Drive", "Images", or "External
Storage" folders when the `destinationType` is `FILE_URI`. However, the user will be able to correctly select any pictures
if they go through the "Gallery" app first. Potential workarounds for this issue are documented on [this StackOverflow question](http://stackoverflow.com/questions/19834842/android-gallery-on-kitkat-returns-different-uri-for-intent-action-get-content/20177611). Please see [CB-5398](https://issues.apache.org/jira/browse/CB-5398) to track this issue.
Android uses intents to launch the camera activity on the device to capture Android uses intents to launch the camera activity on the device to capture
images, and on phones with low memory, the Cordova activity may be killed. In this images, and on phones with low memory, the Cordova activity may be killed. In this
scenario, the image may not appear when the Cordova activity is restored. scenario, the image may not appear when the Cordova activity is restored.

View File

@ -133,14 +133,13 @@ module.exports = {
// because of asynchronous method, so let the successCallback be called in it. // because of asynchronous method, so let the successCallback be called in it.
var resizeImageBase64 = function (file) { var resizeImageBase64 = function (file) {
var imgObj = new Image();
var success = function (fileEntry) { Windows.Storage.FileIO.readBufferAsync(file).done( function(buffer) {
var successCB = function (filePhoto) { var strBase64 = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer);
var fileType = file.contentType, var imageData = "data:" + file.contentType + ";base64," + strBase64;
reader = new FileReader();
reader.onloadend = function () {
var image = new Image(); var image = new Image();
image.src = reader.result; image.src = imageData;
image.onload = function() { image.onload = function() {
var imageWidth = targetWidth, var imageWidth = targetWidth,
@ -154,34 +153,14 @@ module.exports = {
ctx.drawImage(this, 0, 0, imageWidth, imageHeight); ctx.drawImage(this, 0, 0, imageWidth, imageHeight);
// The resized file ready for upload // The resized file ready for upload
var finalFile = canvas.toDataURL(fileType); var finalFile = canvas.toDataURL(file.contentType);
// Remove the prefix such as "data:" + contentType + ";base64," , in order to meet the Cordova API. // Remove the prefix such as "data:" + contentType + ";base64," , in order to meet the Cordova API.
var arr = finalFile.split(","); var arr = finalFile.split(",");
var newStr = finalFile.substr(arr[0].length + 1); var newStr = finalFile.substr(arr[0].length + 1);
successCallback(newStr); successCallback(newStr);
}; };
};
reader.readAsDataURL(filePhoto);
};
var failCB = function () {
errorCallback("File not found.");
};
fileEntry.file(successCB, failCB);
};
var storageFolder = Windows.Storage.ApplicationData.current.localFolder;
file.copyAsync(storageFolder, file.name, Windows.Storage.NameCollisionOption.replaceExisting).then(function (storageFile) {
success(new FileEntry(storageFile.name, "ms-appdata:///local/" + storageFile.name));
}, function () {
fail(FileError.INVALID_MODIFICATION_ERR);
}, function () {
errorCallback("Folder not access.");
}); });
}; };
if (sourceType != Camera.PictureSourceType.CAMERA) { if (sourceType != Camera.PictureSourceType.CAMERA) {

View File

@ -343,19 +343,9 @@ namespace WPCordovaClassLib.Cordova.Commands
try try
{ {
//use photo's actual width & height if user doesn't provide width & height // Resize photo and convert to JPEG
if (cameraOptions.TargetWidth < 0 && cameraOptions.TargetHeight < 0)
{
int streamLength = (int)stream.Length;
imageContent = new byte[streamLength + 1];
stream.Read(imageContent, 0, streamLength);
}
else
{
// resize photo
imageContent = ResizePhoto(stream); imageContent = ResizePhoto(stream);
} }
}
finally finally
{ {
stream.Dispose(); stream.Dispose();
@ -368,7 +358,6 @@ namespace WPCordovaClassLib.Cordova.Commands
/// Resize image /// Resize image
/// </summary> /// </summary>
/// <param name="stream">Image stream</param> /// <param name="stream">Image stream</param>
/// <param name="fileData">File data</param>
/// <returns>resized image</returns> /// <returns>resized image</returns>
private byte[] ResizePhoto(Stream stream) private byte[] ResizePhoto(Stream stream)
{ {
@ -382,10 +371,22 @@ namespace WPCordovaClassLib.Cordova.Commands
WriteableBitmap objWB = new WriteableBitmap(objBitmap); WriteableBitmap objWB = new WriteableBitmap(objBitmap);
objBitmap.UriSource = null; objBitmap.UriSource = null;
// Calculate resultant image size
int width, height;
if (cameraOptions.TargetWidth >= 0 && cameraOptions.TargetHeight >= 0)
{
// Keep proportionally // Keep proportionally
double ratio = Math.Min((double)cameraOptions.TargetWidth / objWB.PixelWidth, (double)cameraOptions.TargetHeight / objWB.PixelHeight); double ratio = Math.Min(
int width = Convert.ToInt32(ratio * objWB.PixelWidth); (double)cameraOptions.TargetWidth / objWB.PixelWidth,
int height = Convert.ToInt32(ratio * objWB.PixelHeight); (double)cameraOptions.TargetHeight / objWB.PixelHeight);
width = Convert.ToInt32(ratio * objWB.PixelWidth);
height = Convert.ToInt32(ratio * objWB.PixelHeight);
}
else
{
width = objWB.PixelWidth;
height = objWB.PixelHeight;
}
//Hold the result stream //Hold the result stream
using (MemoryStream objBitmapStreamResized = new MemoryStream()) using (MemoryStream objBitmapStreamResized = new MemoryStream())