由于权限问题(不允许将数据写入设备)而崩溃。
研究如何通过 AIR/AS3 或编码工具 (IDE) 选项允许此类权限用于输出。
同时考虑:
- 添加
import flash.net.FileReference;。
-
确保在函数之外声明变量。
否则var qfile:FileReference只会在函数触发时才开始存在。这对于桌面上的 Flash Player 来说很好,但在移动设备上,您的应用程序必须与 Android 操作系统对话,而该操作系统可能不喜欢某些(以前)突然出现在函数中的未声明的文件系统编辑变量......
李>
:FileReference 在桌面上弹出一个“另存为”对话框,这在移动设备上是如何工作的?也许您应该(如预期的那样)使用 AIR 的 FileSystem API 来制作 :File 类型,以便在单击“保存”按钮时自动保存到 SD 卡?
查看这些search results 以获取有关其他人如何操作的提示。
(更新代码):未经测试,但首先尝试设置如下代码...
(如果不起作用,则研究权限问题,也可以尝试File 而不是FileReference)
//## 1) add imports
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.display.DisplayObject;
import com.adobe.images.JPGEncoder;
import flash.net.FileReference; //add this
//## 2) declare variables
var names:String = "Image_name";
var qImageData:BitmapData;
var qEncoder:JPGEncoder;
var qBytes:ByteArray;
//var qfile:FileReference = new FileReference();
var qFile:File = File.userDirectory.resolvePath(names+".jpg");
var qFStream:FileStream = new FileStream();
//## 3) add any required listeners
btn_save.addEventListener(MouseEvent.CLICK, save_image);
//## 4) declare all functions here onwards
function save_image(e:MouseEvent):void
{
btn_back.alpha = 0.4; //btn_back.visible = false;
btn_save.alpha = 0.4; //btn_save.visible = false;
qImageData = new BitmapData(540, 960);
qImageData.draw(stage);
qEncoder = new JPGEncoder(100);
qBytes = qEncoder.encode(qImageData);
//var qfile:FileReference = new FileReference();
//var names:String = "Image_name";
//qfile.save(qBytes, names+".jpg");
qBytes.position = 0;
qFStream.open(qFile, FileMode.WRITE);
qFStream.writeBytes(qBytes, 0, qBytes.length);
qFStream.close();
btn_back.alpha = 0.4; //btn_back.visible = true;
btn_save.alpha = 0.4; //btn_save.visible = true;
//## Check if file (qFile) was created successfully (it exists on SD card)
if (qFile.exists)
{
//# Do something if "The File Exists" on the SD card...
}
else
{
//# Here will handle case of "No File Created"...
}
}
还要确保您的Android manifest allows 具有WRITE 权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
这将是一个名为 app.xml 或 YourFileName-app.xml 的文件。确保它是这样的:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<application xmlns="http://ns.adobe.com/air/application/2.5">
<id>author.YourFileName</id>
<versionNumber>1.0.0</versionNumber>
<versionLabel/>
<filename>YourFileName</filename>
<description/>
<!-- To localize the description, use the following format for the description element.<description><text xml:lang="en">English App description goes here</text><text xml:lang="fr">French App description goes here</text><text xml:lang="ja">Japanese App description goes here</text></description>-->
<name>YourFileName</name>
<!-- To localize the name, use the following format for the name element.<name><text xml:lang="en">English App name goes here</text><text xml:lang="fr">French App name goes here</text><text xml:lang="ja">Japanese App name goes here</text></name>-->
<copyright/>
<initialWindow>
<content>YourFileName.swf</content>
<systemChrome>standard</systemChrome>
<transparent>false</transparent>
<visible>true</visible>
<fullScreen>true</fullScreen>
<renderMode>gpu</renderMode>
<autoOrients>true</autoOrients>
</initialWindow>
<icon>
<image72x72>My_Assets/icons/YourFileName_icon_72.png</image72x72>
<image48x48>My_Assets/icons/YourFileName_icon_48.png</image48x48>
<image36x36>My_Assets/icons/YourFileName_icon_36.png</image36x36>
</icon>
<customUpdateUI>false</customUpdateUI>
<allowBrowserInvocation>false</allowBrowserInvocation>
<android>
<manifestAdditions>
<![CDATA[<manifest>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>]]>
</manifestAdditions>
</android>
</application>