Air / Android - 下载并加载png文件

时间:2013-06-06 11:45:41

标签: android

我需要下载.png文件的代码,然后将其加载到舞台上。

以下代码是我正在使用此功能的初始测试版本,它可以在我的电脑上运行,但不能在我的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);
    }

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,正如预期的那样:

localLoader.load(new URLRequest(saveStr));

它需要是:

localLoader.load(new URLRequest(“file://”+ saveStr));