我需要能够下载 .png 文件然后将其加载到舞台的代码。
下面的代码是我对该功能进行的初始测试版本,它可以在我的 PC 上运行,但不能在我的 Android 平板电脑上运行,并产生以下输出和错误:
onDownload_ResponseStatusHandler :: 200
onDownload_CompleteHandler
writeBytesToFile
文件下载成功
ioErrorHandler: [IOErrorEvent type="ioError" bubble=false cancelable=false eventPhase=2 text="Error #2035" errorID=2035]
private function download(url:String):void {
var urlRequest:URLRequest = new URLRequest();
urlRequest.url = url;
var urlStream:URLStream = new URLStream();
urlStream.addEventListener(Event.COMPLETE,onDownload_CompleteHandler);
urlStream.addEventListener(IOErrorEvent.IO_ERROR, onDownload_IOErrorHandler);
urlStream.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS,onDownload_ResponseStatusHandler);
urlStream.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onDownload_SecurityHandler);
urlStream.load(urlRequest);
}
protected function onDownload_CompleteHandler(event:Event):void {
trace("onDownload_CompleteHandler");
var urlStream:URLStream = event.currentTarget as URLStream;
var outBytes:ByteArray = new ByteArray;
urlStream.readBytes(outBytes,0,urlStream.bytesAvailable);
writeBytesToFile(outBytes);
}
private function writeBytesToFile(outBytes:ByteArray):void {
trace("writeBytesToFile");
saveStr = File.applicationStorageDirectory.nativePath+"/loginPlay_ro.png";
var fileToWrite:File = File.applicationStorageDirectory.resolvePath(saveStr);//Where you want store
var fileStream:FileStream = new FileStream();
fileStream.addEventListener(Event.CLOSE, onFileStream_CloseHandler);
fileStream.addEventListener(IOErrorEvent.IO_ERROR,onFileStream_IOErrorHandler);
fileStream.openAsync(fileToWrite, FileMode.WRITE);
fileStream.writeBytes(outBytes, 0, outBytes.length);
fileStream.close();
}
protected function onFileStream_CloseHandler(event:Event = null):void {
trace("File successfully downloaded ");
fileComplete();
}
protected function onFileStream_IOErrorHandler(event:IOErrorEvent):void {
trace("File can't downloaded file doesn't exist ");
}
protected function onDownload_SecurityHandler(event:SecurityErrorEvent):void{
trace("onDownload_SecurityHandler :: " + event.text);
}
protected function onDownload_ResponseStatusHandler(event:HTTPStatusEvent):void{
trace("onDownload_ResponseStatusHandler :: " + event.status);
}
protected function onDownload_IOErrorHandler(event:IOErrorEvent):void {
trace("onDownload_IOErrorHandler :: " + event.text);
}
private function fileComplete(e:Event=null){
var localLoader = new Loader();
localLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, localLoaded);
localLoader.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
localLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
localLoader.load(new URLRequest(saveStr));
}
private function localLoaded(e:Event){
trace("localLoaded");
localHolder.addChild(e.target.content);
e.currentTarget.removeEventListener(Event.COMPLETE, localLoaded);
}
private function initHandler(event:Event):void {
var loader:Loader = Loader(event.target.loader);
var info:LoaderInfo = LoaderInfo(loader.contentLoaderInfo);
trace("initHandler: loaderURL=" + info.loaderURL + " url=" + info.url);
}
private function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
}