5

JavaScript を使用して Windows 8 アプリケーションを開発しています。特定のディレクトリにあるすべての写真 (すべての写真のパス) を取得する必要があります。どうすればいいですか?

私は次のものを持っています:

(function () {
    "use strict";
    var page = WinJS.UI.Pages.define("/html/scenario3.html", {
        ready: function (element, options) {
            document.getElementById("folder").addEventListener("click", pickFolder, false);
        }
    });

    function pickFolder() {
        // Clean scenario output
        WinJS.log && WinJS.log("", "sample", "status");

        // Verify that we are currently not snapped, or that we can 
        // unsnap to open the picker
        var currentState = Windows.UI.ViewManagement.ApplicationView.value;
        if (currentState === Windows.UI.ViewManagement.ApplicationViewState.snapped && !Windows.UI.ViewManagement.ApplicationView.tryUnsnap()) {
            // Fail silently if we can't unsnap
            return;
        }

        // Create the picker object and set options
        var folderPicker = new Windows.Storage.Pickers.FolderPicker;
        folderPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.desktop;
        // Users expect to have a filtered view of their folders depending on the scenario.
        // For example, when choosing a documents folder, restrict the filetypes to documents for your application.
        folderPicker.fileTypeFilter.replaceAll(["*"]);
        //folderPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);

        folderPicker.pickSingleFolderAsync().then(function (folder) {
            if (folder) {
                // Application now has read/write access to all contents in the picked folder (including sub-folder contents)
                // Cache folder so the contents can be accessed at a later time
                Windows.Storage.AccessCache.StorageApplicationPermissions.futureAccessList.addOrReplace("PickedFolderToken", folder);

                var pictures = Windows.Storage.KnownFolders.folder;
                pictures.getFilesAsync().done(function (images) {
                    console.log(images.length + " images found.");
                    WinJS.log && WinJS.log("Total Images: " + images.length, "sample", "status");
                });


            } else {
                // The picker was dismissed with no selected file
                WinJS.log && WinJS.log("Operation cancelled.", "sample", "status");
            }

        });
    }
})();

上記のコードを使用すると、次のエラーが発生します。

Unable to get property 'getFilesAsync' of undefined or null reference

4

1 に答える 1

4

画像ライブラリから画像を読み込む必要がある場合は、まずpackage.appxmanifest、プロジェクトのファイル内の [機能] タブでこの機能が有効になっていることを確認する必要があります。

有効にすると、次の方法でこのライブラリを参照できます。

// 'pictures' is a reference to our Pictures Library
var pictures = Windows.Storage.KnownFolders.picturesLibrary;

Microsoft は、50 ミリ秒を超える操作は非同期にする必要があると判断しました。したがって、このライブラリから画像を取得するには、次の非同期メソッドを使用し、promise が解決されたときに結果を処理する必要があります。

// Get a list of files ('images') within our Pictures Library
pictures.getFilesAsync().done(function( images ) {
    console.log( images.length + " images found." );
});

ここから、見つかった画像を繰り返し処理し、好きなように処理できます。

詳細については、Window.Storage名前空間を確認してください。

さらに、ファイル アクセスのサンプルも役立つ場合があります。

アップデート

更新されたコードから、ユーザーが選択したフォルダーを選択することを許可していることがわかります。これを使用すると、約束が解決されたら、を設定しfileTypeFilteringて画像をプルする必要があります。pickSingleFolderAsync

// Prompt user to select a folder
var picker = Windows.Storage.Pickers.FolderPicker();

// Which file types will we be showing?
picker.fileTypeFilter.append(".jpg");

// Grab the selected folder, reference as 'folder'
picker.pickSingleFolderAsync().then(function (folder) {
    // Get files within selected folder as 'files'
    folder.getFilesAsync().then(function (files) {
        // Output number of files found
        console.log(files.length + " files found.");
    });
});
于 2012-11-12T18:54:29.080 に答える