我正在使用actionscript 3在Air中制作Android应用程序。我需要从服务器下载文件并将其保存到移动SD卡。以下是我的代码,文件从服务器加载,但如何将其保存在SD卡上?
import flash.filesystem.*;
var urlString:String = "http://www.mywebsite.com/xyz/myswftodownload.swf";
var urlReq:URLRequest = new URLRequest(urlString);
var urlStream:URLStream = new URLStream();
var fileData:ByteArray = new ByteArray();
urlStream.addEventListener(ProgressEvent.PROGRESS, onProgress);
urlStream.addEventListener(Event.COMPLETE, loaded);
urlStream.load(urlReq);
function loaded(event:Event):void
{
urlStream.readBytes(fileData, 0, urlStream.bytesAvailable);
trace("Loaded");
urlStream.removeEventListener(ProgressEvent.PROGRESS, onProgress);
urlStream.removeEventListener(Event.COMPLETE, loaded);
//NOW HOW TO SAVE THE LOADED FILE ON SDCARD??
}
function onProgress(evt:ProgressEvent):void {
var nPercent:Number = Math.round((evt.bytesLoaded / evt.bytesTotal) * 100);
//loadingAnim.bar.scaleX = nPercent / 100;
status.text = nPercent.toString() + "%";
}
答案 0 :(得分:1)
你可以做这样的事情
//NOW HOW TO SAVE THE LOADED FILE ON SDCARD
var file:File = File.userDirectory.resolvePath("myswftodownload.swf");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(fileData, 0, fileData.length);
fileStream.close();
File.userDirectory通常指向sdcard(外部存储)。但请注意,外部存储不必始终可用(例如,当设备通过USB连接为大容量存储时)。
在您的情况下,我会考虑使用内部存储,并使用File.applicationDirectory或File.applicationStorageDirectory来保存您的swf文件。外部存储更适合保存图像,音乐,文档等文件。