[英]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.