c# - 如果我使用 getFileAsync 而不是 Picker.pickSingleFileAsync,则无法从 FutureAccessList 检索文件

标签 c# javascript windows windows-store-apps

我有一个应用程序,可以通过两种方式检索文件:一种使用文件选择器,另一种直接使用路径。在这两种情况下,我都会获取文件,将其添加到 future 的访问列表中,并将 token 保存在数组中。稍后在应用程序中,我使用 token 通过 futureAccessList.getFileAsync 检索文件。现在,第二部分,即使用 token 代码取回文件在两种情况下都是相同的,所以它一定是我将其添加到 future 访问列表的方式,因为它在我使用文件选择器时有效,但不是当我直接使用路径时。

文件选择器添加代码

    // Create the picker object and set options
    var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
    openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;
    openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.musicLibrary;

    // Select MIDI files only
    openPicker.fileTypeFilter.replaceAll([".mp3"]);

    // Open the picker for the user to pick a file
    openPicker.pickSingleFileAsync().then(function (file) {
        if (file) {
            // Application now has read/write access to the picked file
            WinJS.log && WinJS.log("Picked file: " + file.name, "sample", "status");

            // Store the file to access again later
            var listToken = Windows.Storage.AccessCache.StorageApplicationPermissions.futureAccessList.add(file);

            // Save the file mapping. If it exists, overwrite.
            fileMappings[padId] = { padNumber: padId, audioFile: listToken };
        }
        else {
            // The picker was dismissed with no selected file
            WinJS.log && WinJS.log("Operation cancelled.", "sample", "status");
        }
    });

直接路径添加代码

    Windows.Storage.KnownFolders.musicLibrary.getFileAsync("filename.mp3").then(function (file) {
        if (file) {
            // Application now has read/write access to the picked file
            WinJS.log && WinJS.log("Picked file: " + file.name + ", full path: " + file.path, "sample", "status");

            // Store the file to access again later
            var listToken = Windows.Storage.AccessCache.StorageApplicationPermissions.futureAccessList.add(file);

            // Save the file mapping. If it exists, overwrite.
            fileMappings['pad0'] = { padNumber: 'pad0', audioFile: listToken };
        }
        else {
            // Could not get access to the file
            WinJS.log && WinJS.log("File unavailable!", "sample", "status");
        }
    });

就我个人而言,我觉得这一行(Windows.Storage.KnownFolders.musicLibrary.getFileAsync)并没有给我读写访问权限,只是读取,这可能就是搞砸的原因。这是a related thread on msdn 。这是一个 C#-MediaElement 问题,但密切相关。知道这里可能出了什么问题吗?如果它与权限相关,我如何指定我需要读写访问权限?我认为在 appxmanifest 中指定功能就足够了。如果有人需要的话,我可以在这里添加文件检索代码。感谢您抽出时间。

[使用 Javascript 的 Windows RT 应用程序。检查确认的“音乐库”功能。]

最佳答案

您的代码是正确的。这是 WinRT API 中的一个错误。我负责系统的这一部分,并提交了有关该主题的错误。

关于c# - 如果我使用 getFileAsync 而不是 Picker.pickSingleFileAsync,则无法从 FutureAccessList 检索文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24966568/

相关文章:

c# - INSERT 语句影响 1 行,但 VS Server Explorer 显示它实际上并没有更新 SQL CE 数据库

c# - 在 Unity3d 中为 VR 360 度图像应用更改聚焦球体的 Material

javascript - SignalR 多用户实时聊天去同步化

c# - 在属性中使用配置设置

javascript - 提交按钮上的单击事件中的 AJAX

python - 如何在不锁定 Vim 的情况下运行当前的 python 脚本?

javascript - 该模式如何获得循环引用

javascript - 谷歌图表 : Line graph + points?

windows - Tomcat/Windows下Jenkins中无法删除pom.xml

c++ - 从 Qt 应用程序内部模拟 TAB 或 SPACE 按键