如何使用 AIR 将图像写入应用程序目录?
我试图通过打开文件流并从位图数据写入字节来将图像保存到应用程序目录。我添加了一些事件侦听器来测试文件流过程,但我没有收到任何响应事件。你能看一下我的代码并告诉我哪里可能有问题吗?
var bd:BitmapData = new BitmapData(CANVAS_WIDTH, CANVAS_HEIGHT);
bd.draw(currentDrawing);
var jpgEncoder:JPGEncoder = new JPGEncoder(100);
var ba:ByteArray = jpgEncoder.encode(bd);
var newImage:File = File.applicationDirectory.resolvePath("images/test.jpg");
var fileStream:FileStream = new FileStream();
fileStream.open(newImage, FileMode.UPDATE);
fileStream.writeBytes(ba);
fileStream.addEventListener(Event.CLOSE, fileClosed);
fileStream.addEventListener(Event.COMPLETE, fileComplete);
fileStream.addEventListener(IOErrorEvent.IO_ERROR, fileError);
fileStream.close();
function fileClosed(event:Event):void {
outputText.text = "close";
}
function fileComplete(event:Event):void {
outputText.text = "complete";
}
function fileError(event:IOErrorEvent):void {
outputText.text = "error";
}
I'm trying to save an image to the application directory by opening a file stream and write the bytes from a bitmap data. I added some events listeners for testing the file stream process but I don't receive any response event. Can you take a look over my code and tell me where might be the problem.
var bd:BitmapData = new BitmapData(CANVAS_WIDTH, CANVAS_HEIGHT);
bd.draw(currentDrawing);
var jpgEncoder:JPGEncoder = new JPGEncoder(100);
var ba:ByteArray = jpgEncoder.encode(bd);
var newImage:File = File.applicationDirectory.resolvePath("images/test.jpg");
var fileStream:FileStream = new FileStream();
fileStream.open(newImage, FileMode.UPDATE);
fileStream.writeBytes(ba);
fileStream.addEventListener(Event.CLOSE, fileClosed);
fileStream.addEventListener(Event.COMPLETE, fileComplete);
fileStream.addEventListener(IOErrorEvent.IO_ERROR, fileError);
fileStream.close();
function fileClosed(event:Event):void {
outputText.text = "close";
}
function fileComplete(event:Event):void {
outputText.text = "complete";
}
function fileError(event:IOErrorEvent):void {
outputText.text = "error";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
AIR 安全性禁止写入应用程序目录。
如果您绝对需要将一些文件写入应用程序目录,则可以使用本机帮助程序来执行此操作(例如,Windows 上的 cmd.exe)。但请考虑使用 File.applicationStorageDirectory 代替 - 它是可写的,但隐藏在用户配置文件中的某个位置。
AIR security forbids writing into app directory.
If you absolutely need to write some file into app dir, you can use native helper to do that (cmd.exe on Windows, for example.) But consider using File.applicationStorageDirectory instead - it is writable, but hidden somewhere in user profile.
flash.fileSystem.File.applicationDirectory< /a> 是只读的。这是与 AIR 应用程序捆绑的资产将显示的位置。
对于特定于您的应用程序的读写访问权限,请使用 flash.fileSystem.File.applicationStorageDirectory 改为。您还可以通过 文件的静态属性。
此 SO 线程列出了 Windows、Mac 和 Linux 的 applicationStorageDirectory 的特定位置: 空气中文件存储位置的区别
flash.fileSystem.File.applicationDirectory is read-only. this is where assets you've bundled with your AIR application will appear.
for read-write access specific to your application, use flash.fileSystem.File.applicationStorageDirectory instead. you also have access to other OS specific read-write directories via File's static properties.
this SO thread lists the specific locations of the applicationStorageDirectory for Windows, Mac and Linux: The Difference Of Location Storage File in air
有一种方法可以写入 applicationDirectory 这是黑客。
there is a way to write to the applicationDirectory here's the hack.
因为您使用的是常规
open()
方法,所以您正在以同步模式打开文件,这意味着代码中的下一行(或语句)将在前一行完成之前不会执行。这意味着您的事件侦听器直到 writeBytes() 完成后才添加。尝试简单地重新排序您的代码行,如下所示:
这将确保在执行(并完成) writeBytes() 方法之前添加事件侦听器,以便调用您的事件处理程序。
但是,请考虑您是否真的需要事件侦听器。事件侦听器(以及它们所基于的“观察者”模式)通常是必须等待异步操作完成的解决方案。由于您只是使用同步写入,因此您可能甚至不需要它们。
Because you are using the regular
open()
method, you are opening the file in synchronous mode, which means that the next line (or statement rather) in your code will not be executed until the previous finishes. This means that your event listeners aren't even added until after writeBytes() finishes.Try to simply reorder your lines of code like so:
This will make sure that the event listeners have been added before the writeBytes() method is executed (and finishes) so your event handlers will be invoked.
However, consider whether you actually need event listeners at all. Event listeners (and the "Observer" pattern which they are built upon) are generally a solution to having to wait for an asynchronous operation to finish. Since you are just using synchronous writing you probably don't even need them.