0

Air+AS3 for Android を使用する場合、サーバーから png をダウンロードするにはどうすればよいですか?

また、後で再度ダウンロードせずに使用できるように、どこに保存しますか?

よろしくミルザ

4

1 に答える 1

0
  1. セキュリティ(アクセス拒否)の問題を回避できるように、ファイルを ApplicationStoragePath に保存することをお勧めします。
  2. writeBytesToFile() で、ファイル名拡張子を含むファイル パスも指定する必要があることを確認してください (「images/first.png」など)。

    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
    {
        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
    {
        var fileToWrite:File = File.applicationStorageDirectory.resolvePath("path/to/store/filenamewithextension");//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 ");                 
    }
    
    protected function onFileStream_IOErrorHandler(event:IOErrorEvent):void
    {
        trace("File can't downloaded file doesn't exist ");
    }
    
    protected function onDownload_SecurityHandler(event:SecurityErrorEvent):void
    {
        logger.error("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);            
    }
    
于 2012-11-18T07:38:38.073 に答える