0
A
回答
0
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:
- Working with File objects in AIR
- Image as ByteArray in Flex
- Image Control
- Programatically load images in Flex。
注意:我想只能用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);
}
相關問題
- 1. 加載未知數量的圖像
- 2. UIL:從外部存儲負載圖像
- 3. swaping外部圖像(負載 - >刪除 - >重負載)
- 4. Jquery的負載()未加載圖像
- 5. iPhone負載部分圖像
- 6. 顯示未知數量的圖像
- 7. Openlayers矢量圖層未知寬度的外部圖形
- 8. ajax無法加載外部圖像
- 9. PHP從函數內部訪問未知的外部變量
- 10. 無法從AS3中的狀態加載外部XML中的圖像
- 11. 檢索未知數量的XML屬性
- 12. SSL加載外部圖像
- 13. ImageData的外部加載的圖像?
- 14. 嵌入負載的圖像
- 15. 將外部圖像加載到gridview(無圖像)
- 16. AS3 XML負載 - 變量不裝載功能外存在
- 17. 負載圖像getRealPath()
- 18. 導致大量負載的php通知
- 19. 加載外部xml
- 20. 負載的外部JS,移動到
- 21. Django XML無法加載外部實體
- 22. 無法加載MVC3中的外部圖像列表
- 23. AS3中的外部加載圖像無法點擊?
- 24. 未知數量的表的SQLite圖ERD
- 25. 列出未知數量的子目錄中的圖像
- 26. jqGrid的XML數據的屬性負載
- 27. UIL從外部URL加載AVD中的圖像,但未加載設備
- 28. GCM未在通知中使用來自JSON負載的數據
- 29. 負載從XML
- 30. 使用未知數量的視圖(Swift)
本地文件夾,遠程文件夾?你有什麼嘗試?你看過flash.filesystem.File上的flash文檔嗎? –
感謝您的回覆,我嘗試從本地文件夾加載外部圖像。 – Nesrin
@Nesrin您的Web或桌面(空氣)項目? –