[英]How to save an image in Windows Phone 8.1 using WinJS
我希望使用WinJS將Windows 8應用程序遷移到Windows Phone 8.1。 我在Windows 8中使用了picker.pickSaveFileAsync
,但WP 8.1不支持。
然后,我從http://code.msdn.microsoft.com/windowsapps/Simple-Imaging-Sample-a2dec2b0引用了官方示例
單擊“另存為”按鈕時,示例中的Javascript版本不會保存在Windows Phone 8.1上,並且在調用`getFileAsync時,它將返回以下錯誤:
0x80004005-JavaScript運行時錯誤:未指定的錯誤
當點擊保存時,它返回一個只讀錯誤。 我也在Lumia 520中測試了樣品。 我在電話中遇到了同樣的錯誤。
在Windows Phone中,您無法獲得對從FileOpenPicker返回的文件的寫訪問權。 您必須使用FileSavePicker來執行此操作。 在同事的幫助下,我獲得了可以打開的示例工作,然后從“空白” Windows Phone應用程序模板開始以新名稱重新保存文件。
在default.html內部,創建兩個按鈕:
<button id="choose">Choose a Photo</button>
<button id="save">Save a Photo</button>
將default.js替換為以下內容:
(function () {
"use strict";
var app = WinJS.Application;
var activation = Windows.ApplicationModel.Activation;
var origFile = null;
function pickPhoto() {
var picker = new Windows.Storage.Pickers.FileOpenPicker();
var enumerator = Windows.Graphics.Imaging.BitmapDecoder.getDecoderInformationEnumerator();
enumerator.forEach(function (decoderInfo) {
decoderInfo.fileExtensions.forEach(function (fileExtension) {
picker.fileTypeFilter.append(fileExtension);
});
});
picker.pickSingleFileAndContinue();
}
function loadPhoto(file) {
origFile = file;
}
function savePhotoPicker(file) {
var picker = new Windows.Storage.Pickers.FileSavePicker();
picker.fileTypeChoices.insert("JPEG file", [".jpg"]);
picker.pickSaveFileAndContinue();
}
function savePhoto(src, dest) {
src.copyAndReplaceAsync(dest).done(function () {
console.log("success");
})
}
app.onactivated = function (args) {
if (args.detail.kind === activation.ActivationKind.launch) {
if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
// TODO: This application has been newly launched. Initialize
// your application here.
} else {
// TODO: This application has been reactivated from suspension.
// Restore application state here.
}
args.setPromise(WinJS.UI.processAll());
document.getElementById("choose").addEventListener("click", pickPhoto);
document.getElementById("save").addEventListener("click", savePhotoPicker);
}
if (args.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.pickFileContinuation) {
loadPhoto(args.detail.files[0]);
}
if (args.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.pickSaveFileContinuation) {
savePhoto(origFile, args.detail.file);
}
};
app.oncheckpoint = function (args) {
// TODO: This application is about to be suspended. Save any state
// that needs to persist across suspensions here. You might use the
// WinJS.Application.sessionState object, which is automatically
// saved and restored across suspension. If you need to complete an
// asynchronous operation before your application is suspended, call
// args.setPromise().
};
app.start();
})();
如果您自己創建文件,請確保在保存之前為其設置ImageProperties 。
抱歉,損壞的樣品我會通知樣品所有者。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.