load unknown number of external images without XML

☆樱花仙子☆ 提交于 2019-12-25 05:26:12

问题


I need to know how to load unknown number of images from external folder without using XML

help me please, thank you


回答1:


So from your comments I'm assuming this is an AIR Application, so you can access the filesystem via File class.

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).

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");

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);
}



回答2:


Can you elaborate a little bit better the question? If it's a user action (i.e. the user needs to upload some photos) I'd use the File API - you can see examples here - otherwise, if it's from the server side I'd use a PHP or Phyton script.




回答3:


Assuming that your application is Air (Desktop application), this code will be useful:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
    <mx:Script>
        <![CDATA[
            public function init():void{
                var fs:FileStream = new FileStream();
                var f:File = new File("c:/imgTest.jpg");
                var b:ByteArray = new ByteArray();
                fs.open(f, FileMode.READ);
                fs.readBytes(b,0,fs.bytesAvailable);
                idImg.source  = b;
                fs.close();
            }
        ]]>
    </mx:Script>
    <mx:Image id="idImg" width="100%" height="100%"/>
</mx:WindowedApplication>

Place an image in c:/imgTest.jpg. Note that this image is outside from your project path. You have other options to load images, but these must be accessible by URL, or should be in the path of the project. Here a links that will be usefull for load images in Flex Air and Web:

  • Working with File objects in AIR
  • Image as ByteArray in Flex
  • Image Control
  • Programatically load images in Flex.

Note: i tried only with JPG files, i do not know if this works with other types.



来源:https://stackoverflow.com/questions/17906780/load-unknown-number-of-external-images-without-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!