CB-2442 CB-2419 Use Windows.Storage.ApplicationData.current.localFolder, instead of writing to app package.

This commit is contained in:
Jesse MacFadyen 2013-12-09 17:35:18 -08:00
parent ccd59e66ba
commit 2437c40fe7

View File

@ -22,6 +22,7 @@
/*global Windows:true, URL:true */ /*global Windows:true, URL:true */
var cordova = require('cordova'), var cordova = require('cordova'),
Camera = require('./Camera'), Camera = require('./Camera'),
FileEntry = require('org.apache.cordova.file.FileEntry'), FileEntry = require('org.apache.cordova.file.FileEntry'),
@ -90,16 +91,23 @@ module.exports = {
// The resized file ready for upload // The resized file ready for upload
var _blob = canvas.msToBlob(); var _blob = canvas.msToBlob();
var _stream = _blob.msDetachStream(); var _stream = _blob.msDetachStream();
Windows.Storage.StorageFolder.getFolderFromPathAsync(packageId.path).done(function (storageFolder) {
storageFolder.createFileAsync(tempPhotoFileName, Windows.Storage.CreationCollisionOption.generateUniqueName).done(function (file) { var storageFolder = Windows.Storage.ApplicationData.current.localFolder;
file.openAsync(Windows.Storage.FileAccessMode.readWrite).done(function (fileStream) { storageFolder.createFileAsync(tempPhotoFileName, Windows.Storage.CreationCollisionOption.generateUniqueName).done(function (file) {
Windows.Storage.Streams.RandomAccessStream.copyAndCloseAsync(_stream, fileStream).done(function () { file.openAsync(Windows.Storage.FileAccessMode.readWrite).done(function (fileStream) {
var _imageUrl = URL.createObjectURL(file); Windows.Storage.Streams.RandomAccessStream.copyAndCloseAsync(_stream, fileStream).done(function () {
successCallback(_imageUrl); var _imageUrl = URL.createObjectURL(file);
}, function () { errorCallback("Resize picture error."); }); successCallback(_imageUrl);
}, function () { errorCallback("Resize picture error."); }); }, function () {
}, function () { errorCallback("Resize picture error."); }); errorCallback("Resize picture error.");
});
}, function () {
errorCallback("Resize picture error.");
});
}, function () {
errorCallback("Resize picture error.");
}); });
}; };
}; };
@ -112,14 +120,13 @@ module.exports = {
fileEntry.file(successCB, failCB); fileEntry.file(successCB, failCB);
}; };
Windows.Storage.StorageFolder.getFolderFromPathAsync(packageId.path).done(function (storageFolder) { var storageFolder = Windows.Storage.ApplicationData.current.localFolder;
file.copyAsync(storageFolder, file.name, Windows.Storage.NameCollisionOption.replaceExisting).then(function (storageFile) { file.copyAsync(storageFolder, file.name, Windows.Storage.NameCollisionOption.replaceExisting).then(function (storageFile) {
success(new FileEntry(storageFile.name, storageFile.path)); success(new FileEntry(storageFile.name, storageFile.path));
}, function () { }, function () {
fail(FileError.INVALID_MODIFICATION_ERR); fail(FileError.INVALID_MODIFICATION_ERR);
}, function () { }, function () {
errorCallback("Folder not access."); errorCallback("Folder not access.");
});
}); });
}; };
@ -165,16 +172,16 @@ module.exports = {
fileEntry.file(successCB, failCB); fileEntry.file(successCB, failCB);
}; };
Windows.Storage.StorageFolder.getFolderFromPathAsync(packageId.path).done(function (storageFolder) { var storageFolder = Windows.Storage.ApplicationData.current.localFolder;
file.copyAsync(storageFolder, file.name, Windows.Storage.NameCollisionOption.replaceExisting).then(function (storageFile) { file.copyAsync(storageFolder, file.name, Windows.Storage.NameCollisionOption.replaceExisting).then(function (storageFile) {
success(new FileEntry(storageFile.name, storageFile.path)); success(new FileEntry(storageFile.name, "ms-appdata:///local/" + storageFile.name));
}, function () { }, function () {
fail(FileError.INVALID_MODIFICATION_ERR); fail(FileError.INVALID_MODIFICATION_ERR);
}, function () { }, function () {
errorCallback("Folder not access."); errorCallback("Folder not access.");
});
}); });
}; };
if (sourceType != Camera.PictureSourceType.CAMERA) { if (sourceType != Camera.PictureSourceType.CAMERA) {
@ -182,9 +189,11 @@ module.exports = {
fileOpenPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary; fileOpenPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
if (mediaType == Camera.MediaType.PICTURE) { if (mediaType == Camera.MediaType.PICTURE) {
fileOpenPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]); fileOpenPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);
} else if (mediaType == Camera.MediaType.VIDEO) { }
else if (mediaType == Camera.MediaType.VIDEO) {
fileOpenPicker.fileTypeFilter.replaceAll([".avi", ".flv", ".asx", ".asf", ".mov", ".mp4", ".mpg", ".rm", ".srt", ".swf", ".wmv", ".vob"]); fileOpenPicker.fileTypeFilter.replaceAll([".avi", ".flv", ".asx", ".asf", ".mov", ".mp4", ".mpg", ".rm", ".srt", ".swf", ".wmv", ".vob"]);
} else { }
else {
fileOpenPicker.fileTypeFilter.replaceAll(["*"]); fileOpenPicker.fileTypeFilter.replaceAll(["*"]);
} }
@ -193,16 +202,16 @@ module.exports = {
if (destinationType == Camera.DestinationType.FILE_URI) { if (destinationType == Camera.DestinationType.FILE_URI) {
if (targetHeight > 0 && targetWidth > 0) { if (targetHeight > 0 && targetWidth > 0) {
resizeImage(file); resizeImage(file);
} else { }
Windows.Storage.StorageFolder.getFolderFromPathAsync(packageId.path).done(function (storageFolder) { else {
file.copyAsync(storageFolder, file.name, Windows.Storage.NameCollisionOption.replaceExisting).then(function (storageFile) {
var _imageUrl = URL.createObjectURL(storageFile); var storageFolder = Windows.Storage.ApplicationData.current.localFolder;
successCallback(_imageUrl); file.copyAsync(storageFolder, file.name, Windows.Storage.NameCollisionOption.replaceExisting).then(function (storageFile) {
}, function () { successCallback(URL.createObjectURL(storageFile));
fail(FileError.INVALID_MODIFICATION_ERR); }, function () {
}, function () { fail(FileError.INVALID_MODIFICATION_ERR);
errorCallback("Folder not access."); }, function () {
}); errorCallback("Folder not access.");
}); });
} }
@ -240,18 +249,24 @@ module.exports = {
} else { } else {
cameraCaptureUI.photoSettings.format = Windows.Media.Capture.CameraCaptureUIPhotoFormat.jpeg; cameraCaptureUI.photoSettings.format = Windows.Media.Capture.CameraCaptureUIPhotoFormat.jpeg;
} }
// decide which max pixels should be supported by targetWidth or targetHeight. // decide which max pixels should be supported by targetWidth or targetHeight.
if (targetWidth >= 1280 || targetHeight >= 960) { if (targetWidth >= 1280 || targetHeight >= 960) {
cameraCaptureUI.photoSettings.maxResolution = Windows.Media.Capture.CameraCaptureUIMaxPhotoResolution.large3M; cameraCaptureUI.photoSettings.maxResolution = Windows.Media.Capture.CameraCaptureUIMaxPhotoResolution.large3M;
} else if (targetWidth >= 1024 || targetHeight >= 768) { }
else if (targetWidth >= 1024 || targetHeight >= 768) {
cameraCaptureUI.photoSettings.maxResolution = Windows.Media.Capture.CameraCaptureUIMaxPhotoResolution.mediumXga; cameraCaptureUI.photoSettings.maxResolution = Windows.Media.Capture.CameraCaptureUIMaxPhotoResolution.mediumXga;
} else if (targetWidth >= 800 || targetHeight >= 600) { }
else if (targetWidth >= 800 || targetHeight >= 600) {
cameraCaptureUI.photoSettings.maxResolution = Windows.Media.Capture.CameraCaptureUIMaxPhotoResolution.mediumXga; cameraCaptureUI.photoSettings.maxResolution = Windows.Media.Capture.CameraCaptureUIMaxPhotoResolution.mediumXga;
} else if (targetWidth >= 640 || targetHeight >= 480) { }
else if (targetWidth >= 640 || targetHeight >= 480) {
cameraCaptureUI.photoSettings.maxResolution = Windows.Media.Capture.CameraCaptureUIMaxPhotoResolution.smallVga; cameraCaptureUI.photoSettings.maxResolution = Windows.Media.Capture.CameraCaptureUIMaxPhotoResolution.smallVga;
} else if (targetWidth >= 320 || targetHeight >= 240) { }
else if (targetWidth >= 320 || targetHeight >= 240) {
cameraCaptureUI.photoSettings.maxResolution = Windows.Media.Capture.CameraCaptureUIMaxPhotoResolution.verySmallQvga; cameraCaptureUI.photoSettings.maxResolution = Windows.Media.Capture.CameraCaptureUIMaxPhotoResolution.verySmallQvga;
} else { }
else {
cameraCaptureUI.photoSettings.maxResolution = Windows.Media.Capture.CameraCaptureUIMaxPhotoResolution.highestAvailable; cameraCaptureUI.photoSettings.maxResolution = Windows.Media.Capture.CameraCaptureUIMaxPhotoResolution.highestAvailable;
} }
@ -263,15 +278,14 @@ module.exports = {
if (targetHeight > 0 && targetWidth > 0) { if (targetHeight > 0 && targetWidth > 0) {
resizeImage(picture); resizeImage(picture);
} else { } else {
Windows.Storage.StorageFolder.getFolderFromPathAsync(packageId.path).done(function (storageFolder) {
picture.copyAsync(storageFolder, picture.name, Windows.Storage.NameCollisionOption.replaceExisting).then(function (storageFile) { var storageFolder = Windows.Storage.ApplicationData.current.localFolder;
var _imageUrl = URL.createObjectURL(storageFile); picture.copyAsync(storageFolder, picture.name, Windows.Storage.NameCollisionOption.replaceExisting).then(function (storageFile) {
successCallback(_imageUrl); successCallback("ms-appdata:///local/" + storageFile.name);
}, function () { }, function () {
fail(FileError.INVALID_MODIFICATION_ERR); fail(FileError.INVALID_MODIFICATION_ERR);
}, function () { }, function () {
errorCallback("Folder not access."); errorCallback("Folder not access.");
});
}); });
} }
} else { } else {
@ -306,15 +320,14 @@ module.exports = {
if (targetHeight > 0 && targetWidth > 0) { if (targetHeight > 0 && targetWidth > 0) {
resizeImage(picture); resizeImage(picture);
} else { } else {
Windows.Storage.StorageFolder.getFolderFromPathAsync(packageId.path).done(function (storageFolder) {
picture.copyAsync(storageFolder, picture.name, Windows.Storage.NameCollisionOption.replaceExisting).then(function (storageFile) { var storageFolder = Windows.Storage.ApplicationData.current.localFolder;
var _imageUrl = URL.createObjectURL(storageFile); picture.copyAsync(storageFolder, picture.name, Windows.Storage.NameCollisionOption.replaceExisting).then(function (storageFile) {
successCallback(_imageUrl); successCallback("ms-appdata:///local/" + storageFile.name);
}, function () { }, function () {
fail(FileError.INVALID_MODIFICATION_ERR); fail(FileError.INVALID_MODIFICATION_ERR);
}, function () { }, function () {
errorCallback("Folder not access."); errorCallback("Folder not access.");
});
}); });
} }
} else { } else {