如何在 AIR 应用程序中动态加载文件?我必须使用 File 类吗?

发布于 2025-01-04 01:10:20 字数 1110 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2025-01-11 01:10:20

如果我正确理解你的问题,我相信你只需要将其加载为数据源/数组,然后你可以将其绑定到你选择的组件。

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.

像你 2025-01-11 01:10:20

文档中指出如果您使用 File 类访问文件,那么这在所有平台上都是安全的。此外,File 对象的 .url 属性将使用适当的 URL 方案...

  • “app:” - 相对于应用程序目录
  • “app-storage:” - 相对于特殊应用程序存储目录
  • “file:” - 所有其他。 ..我想如果你指定一个绝对路径

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

  • "app:" - relative to the application directory
  • "app-storage:" - relative to the special application storage directory
  • "file:" - all else...I think if you specify an absolute path
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文