繁体   English   中英

使用JavaScript从给定目录中获取图片

[英]Get pictures from a given directory using JavaScript

我正在使用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

如果需要从图片库加载图片,首先需要检查项目的package.appxmanifest文件内的Capabilities选项卡中是否启用了此功能。

启用后,您可以使用以下内容引用此库:

// '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 promise解析后拉出图像:

// 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.");
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM