2013-07-28 34 views
0

我需要知道如何加載未知數量的外部文件夾的圖像,而無需使用XML負載未知數量的外部圖像的無XML

請幫助我, 謝謝

+0

本地文件夾,遠程文件夾?你有什麼嘗試?你看過flash.filesystem.File上的flash文檔嗎? –

+0

感謝您的回覆,我嘗試從本地文件夾加載外部圖像。 – Nesrin

+2

@Nesrin您的Web或桌面(空氣)項目? –

回答

0

你能制定一個更好一點這個問題?如果是用戶操作(即用戶需要上傳一些照片),我會使用File API - 您可以看到示例here - 否則,如果它來自服務器端,我會使用PHP或Phyton腳本。

+0

我在桌面應用程序中工作,我需要從包含圖像的外部文件夾中創建圖庫,但我不知道這些圖像的數量,因爲每次都可能更改數量,所以我需要知道如何將它們加載到應用程序中 – Nesrin

0

假設你的應用程序是航(桌面應用程序),該代碼將是有益的:

<?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> 

放置在c。將圖像:/imgTest.jpg。請注意,此圖像位於項目路徑之外。 您可以使用其他選項來加載圖像,但這些選項必須可通過URL訪問,或者應位於項目的路徑中。 這裏一個鏈接,這將是有用的Flex中航載入圖像和Web:

注意:我想只能用JPG文件,我不知道這是否與其他類型。

1

所以從您的意見,我假設這是一個AIR應用程序,所以你可以通過File類訪問文件系統。

首先,你需要得到一個指向你文件夾的File對象,最簡單的方法就是對它進行硬編碼。稍微複雜一點的方法是打開一個對話框,用戶可以選擇他想要的文件夾(使用File.browseForOpen)。

讓我們採取簡單的方法,並定義文件夾以恆定的軌跡,這是一個被稱爲「圖像」中的用戶文件夾的文件夾:

File imageFolder = File.documentsDirectory.resolvePath("images"); 

一旦我們有一個文件夾中的實例,我們可以使用getDirectoryListing方法列出該文件夾中的所有文件。這裏有一個例子:

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

要將文件加載到你的應用程序,你可以做這樣的事情:

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