i made a flash air application for android and i need to download file and save to application local directory
Thanks in advance....
URLRequest and URLLoader will allow you to fetch some data from the internet, FileStream allows you to save it locally (FileReference if you want them to choose where to save it) as in
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="windowedapplication1_creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
{
// TODO Auto-generated method stub
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, complete_handler);
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.load(new URLRequest("http://www.shaunhusain.com/CheckboxList/CheckboxList.swf"));
}
private function complete_handler(event:Event):void
{
var data:ByteArray = event.target.data;
var fr:FileReference = new FileReference();
fr.save(data, 'test.swf');
var fileStream:FileStream = new FileStream();
trace(File.applicationDirectory.nativePath);
fileStream.open(new File(File.applicationDirectory.nativePath+"\\test.swf"),FileMode.WRITE);
fileStream.writeBytes(data, 0, data.length);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
</s:WindowedApplication>
EDIT: AS3 Mobile project version:
package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.net.FileReference;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.utils.ByteArray;
public class DownloadFileMobileAS3 extends Sprite
{
public function DownloadFileMobileAS3()
{
super();
// support autoOrients
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
createLoader();
}
protected function createLoader():void
{
// TODO Auto-generated method stub
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, complete_handler);
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.load(new URLRequest("http://www.shaunhusain.com/CheckboxList/CheckboxList.swf"));
}
private function complete_handler(event:Event):void
{
var data:ByteArray = event.target.data;
var fr:FileReference = new FileReference();
fr.save(data, 'test.swf');
var fileStream:FileStream = new FileStream();
trace("something");
trace(File.applicationDirectory);
trace(File.applicationDirectory.nativePath);
trace(File.applicationStorageDirectory.nativePath);
fileStream.open(new File(File.applicationStorageDirectory.nativePath+"\\test.swf"),FileMode.WRITE);
fileStream.writeBytes(data, 0, data.length);
}
}
}
I included both options in the code above you would want to either take out the file reference part or take out the file stream part otherwise it'll be saved twice.
I just added the mobile section, it in fact does have some sort of dialog it prompts you with for saving a file on Android, though it's a dialog I've never seen before, just gives you the option of where to save it and Audio Files, Image Files, and Video Files as options or a default location at /mnt/sdcard/test.swf. I let it save there and am able to see it using ES Explorer (though I had no good way to verify the data was in tact it appears generally to be correct). The one using the applicationDirectory was failing on me, tracing it out I was seeing no nativePath so I switched it to applicationStorageDirectory which had a value and it seems to have saved without error, unfortunately it's in my root /data folder and since I don't have my phone rooted I have no way to completely confirm the file is actually there but no errors. Let me know if you've tried this and what specific issues you're still encountering.