更新时间:2023-12-05 20:00:46
因此,根据您的评论,我认为这是一个AIR应用程序,因此您可以通过File
类访问文件系统.
So from your comments I'm assuming this is an AIR Application, so you can access the filesystem via File
class.
首先,您需要获取一个指向文件夹的File
对象,最简单的方法是对其进行硬编码.稍微复杂一点的方法将涉及打开一个对话框,用户可以在其中选择所需的文件夹(使用
First of all, you need to get a File
object that points to your folder, the easiest way is to hardcode it. A slightly more complex approach would involve opening a dialog, where the user can select his desired folder (using File.browseForOpen
).
让我们走一条简单的路线,并为该文件夹定义一个恒定路径,这里是用户文档文件夹中的一个名为"images"的文件夹:
Let's take the easy route and define a constant path to the folder, here it's a folder called "images" in the users documents folder:
File imageFolder = File.documentsDirectory.resolvePath("images");
一旦有了文件夹实例,就可以使用getDirectoryListing
方法列出该文件夹中的所有文件.这是一个示例:
Once we have a folder instance, we can use the getDirectoryListing
method to list all files within that folder. Here's an example:
// create a vector that will contain all our images
var images:Vector.<File> = new Vector.<File>();
// first, check if that folder really exists
if(imageFolder.exists && imageFolder.isDirectory){
var files:Array = imageFolder.getDirectoryListing();
for each(var file:File in files){
// match the filename against a pattern, here only files
// that end in jpg, jpeg, png or gif will be accepted
if(file.name.match(/\.(jpe?g|png|gif)$/i)){
images.push(file);
}
}
}
// at this point, the images vector will contain all image files
// that were found in the folder, or nothing if no images were found
// or the folder didn't exist.
要将文件加载到您的应用程序中,可以执行以下操作:
To load the files into your application, you can do something like this:
for each(var file:File in images){
// use a FileStream to read the file data into a ByteArray
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
var bytes:ByteArray = new ByteArray();
stream.readBytes(bytes);
stream.close();
// create a loader and load the image into it
var loader:Loader = new Loader();
// use the loadBytes method to read the data
loader.loadBytes(bytes);
// you can add the loader to the scene, so that it will be visible.
// These loaders will all be at 0, 0 coordinates, so maybe change
// the x and y coordinates to something more meaningful/useful.
this.addChild(loader);
}