如何在 AIR 应用程序中动态加载文件?我必须使用 File 类吗?
我有一个 XML 文件,它指定了我需要加载的图像文件。 XML 文件和图像文件位于相对于 AIR 应用程序所在位置的子文件夹中。
我需要加载 XML 文件和图像(加载它们并将它们作为子项添加到影片剪辑中)
在我的 AIR 应用程序中,当我尝试通过 URLRequest 加载它时,它不起作用。
myLoader = new URLLoader();
myLoader.load(new URLRequest(xmlFilename));
myLoader.addEventListener(Event.COMPLETE, processXML);
(我知道这适用于 .swf b/c 我已经测试过)
我找到了一些使用 File 类的示例,这是我的代码,这确实有效:
var aFile:File = File.applicationDirectory.resolvePath( xmlFilename );
var aStream:FileStream = new FileStream();
aStream.open( aFile, FileMode.READ );
configXML = new XML( aStream.readUTFBytes( aStream.bytesAvailable ) );
aStream.close();
processXML();
...
我现在正在尝试加载图像在 XML 文件中指定,我发现必须使用 File 类来引用文件系统中的图像。
var engImageFile:File = File.applicationDirectory.resolvePath( "./english/"+ engFilename );
ldr = new Loader();
var urlReq:URLRequest = new URLRequest( engImageFile.url );
ldr.contentLoaderInfo.addEventListener( Event.COMPLETE, engImgLoaded );
ldr.load(urlReq);
这是 AIR 在想要读取/加载等时访问文件的方式(使用 File 类)吗?他们?
I have an XML file that specifies the image files that I need to load. The XML file and the image files live in a subfolder relative to where the AIR app lives.
I need to load the XML file and also the images (load them and add them as children to a movieclip)
In my AIR app, when I tried to load it via the URLRequest, it didn't work.
myLoader = new URLLoader();
myLoader.load(new URLRequest(xmlFilename));
myLoader.addEventListener(Event.COMPLETE, processXML);
(I know this works from a .swf b/c I've tested it)
I've found some sample which uses the File class and here's my code and this does work:
var aFile:File = File.applicationDirectory.resolvePath( xmlFilename );
var aStream:FileStream = new FileStream();
aStream.open( aFile, FileMode.READ );
configXML = new XML( aStream.readUTFBytes( aStream.bytesAvailable ) );
aStream.close();
processXML();
...
I'm now trying to load the images specified in the XML file and I'm finding that I have to use the File class to reference the image in the file system.
var engImageFile:File = File.applicationDirectory.resolvePath( "./english/"+ engFilename );
ldr = new Loader();
var urlReq:URLRequest = new URLRequest( engImageFile.url );
ldr.contentLoaderInfo.addEventListener( Event.COMPLETE, engImgLoaded );
ldr.load(urlReq);
Is this the way that AIR accesses files (using the File class) when it wants to read/load/etc. them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我正确理解你的问题,我相信你只需要将其加载为数据源/数组,然后你可以将其绑定到你选择的组件。
if I understand your question correctly, I belive you just need to load it as a data source/array, which you can then bind to a component of your choosing.
在文档中指出如果您使用 File 类访问文件,那么这在所有平台上都是安全的。此外,File 对象的 .url 属性将使用适当的 URL 方案...
In the documentation it states that if you use the File class to access files then this is safe across all platforms. Also the .url property of the File object will use the appropriate URL scheme...