gpt4 book ai didi

javascript - 如何使用 WinJS 在 Windows Phone 8.1 中保存图像

转载 作者:行者123 更新时间:2023-11-28 01:17:21 25 4
gpt4 key购买 nike

我希望使用 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 runtime error: Unspecified error

单击“保存”时,会返回只读错误。我也在 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在保存之前获取它。

很抱歉 sample 损坏了,我会将其报告给 sample 所有者。

关于javascript - 如何使用 WinJS 在 Windows Phone 8.1 中保存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23681662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com