AS3 Air On Android - Load SWF as its own Main() - android

I'm using a wrapper app to download a remote SWF and save it to a Xoom Tablet. The download and save works fine but using something like
var file:File = File.applicationStorageDirectory.resolvePath("app.swf");
var loader:Loader = new Loader();
loader.load(new URLRequest(file.url)/*,context needed?*/);
_main.addChild(loader);
Screws with the targeting in the loaded swf so that in my loaded swf if I try and call "stage" from "Main" its null. I thought since loaded SWF's had there own sandbox it would load and run just like it I installed it by its self, does not seem to be the case. If this was a desktop app I could use NativeWindow but its not supported on mobile devices yet. Are there any know options where I can load and run it in its own sandbox where paths and roots are intact? Possibly a command it launch it as a separate swf from my wrapper swf?

I ran into more issues but finaly found the correct way, you cant acceess "File" but this works just fine.
function getSWF():void
{
var request:URLRequest = new URLRequest("child.swf");
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadHandler);
loader.load(request);
_main.addChild(loader);// _main is my display sprite
}
function loadHandler(event:Event):void
{
var childSwf:Object = event.target.content;
childSwf.init( PARAMS );// in my case I needed to pass XML but this is any method in the Main of the child swf, note that "stage" is read only, so your parent swf needs to set any stage vars you need
}

Related

Air for android ignores external swf files

I am working on some app that loads external swfs from the application directory
the problem is that some swf files get loaded correctly and others give ioerror url not found
i put the paths in an array and use a loader to load the path
var arr:Array = ["Games/1.swf", "Games/2.swf"];
var loader:Loader = new Loader();
loader.load(new URLRequest(arr[0]));
this is just example and it is the same in loading all the files but does not work on all files.
what would be the problem?
Firstly, whenever you load something, listen for the appropriate error events so you know what's going on:
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
When using mobile, you should use the app:// shorthand as a reference to the application directory (which I assume is where your Game folder is. Relative paths do not usually work.
So it should look like this:
var arr:Array = ["app://Games/1.swf", "app://Games/2.swf"];
For more information/options, you can look at my answer to this question

Load an external text. Works on my computer, but not on app

I'm creating an app for Air 3.8 on Flash CS6. When I click on info_btn, the programm load an external textfile (here test.txt) and shows it in my dynamic text field (here info_txt).
It works when I CRTL+Enter, but not on my android device when I test the .apk file I created. I'm sure the reason is that the test.txt is not compiled in the apk, but I don't know how to change that. If you guys can help, it would be great.
Here is my code, if it can help:
var fl_TextLoader_3:URLLoader = new URLLoader();
var fl_TextURLRequest_3:URLRequest = new URLRequest("textes/test.txt");
fl_TextLoader_3.addEventListener(Event.COMPLETE, fl_CompleteHandler_3);
function fl_CompleteHandler_3(event:Event):void
{
var textData:String = new String(fl_TextLoader_3.data);
info_txt.text = textData;
}
info_btn.addEventListener(MouseEvent.CLICK, fl_infotxt);
function fl_infotxt(Event:MouseEvent):void {
fl_TextLoader_3.load(fl_TextURLRequest_3);
}
Usually mobile AIR projects will have an assets folder -- they do in Flash Builder and Flash Develop IDEs, so I assume CS6 has something similar.
The assets folder is where images and app icons go that will be delivered in the .apk. You can put any files and folders you want in there, including your text files. To package your text file into the .apk, you could create a assets/textes folder and put your test.txt in it.
Then the URL you would need in your code would be:
var fl_TextURLRequest_3:URLRequest = new URLRequest("assets/textes/test.txt");

PhoneGap read XML with FileReader?

I have an XML file on the SDCard, that is downloaded from a server. I am trying to open and read in that XML in order to use it within my app. I open and read it fine, but JQuery is freezing on parsing it.
Once I get the file I do like so:
var reader = new FileReader();
reader.onloadend = function (evt) {
parseXML(evt.target.result);
};
reader.readAsText(file);
And then I begin parsing like so:
function parseXML(xml){
$(xml).find('recipe').each(function(){
And it stops there. If I load the in-app XML using JQuery's ajax method it works fine. So, is there anything special about loading XML data using the FileReader in PhoneGap?
UPDATE:
OK, with much time and help from another dev, the issue appears to be a bug in PhoneGap. Specifically the FileTRansfer object.
I was doing this:
var ft = new FileTransfer();
var dlPath = dataDir.fullPath + "/recipes.xml";
ft.download("http://design.mydomain.com/dave/humana/recipes/recipes.xml", dlPath, addedNewRecipes, addError);
And I'd get a trace within addedNewRecipes that the download succeeded. And it did... just not all of it. The file was truncated and that's what was causing the parseXML to fail. The entire XML file is about 90KB (~1600 lines) it was being truncated at maybe 50 lines.
Replaced FileTransfer with a FileWriter object and it works now.
PhoneGap/Cordova really is still beta isn't it.

Flex Air Mobile File Download

I am building an Adobe Air Mobile application currently targeting Android (and eventually iOS). I'm using Flash Builder 4.6 (which is Flex).
I have a requirement to download a zip file from a url, save it to local storage and unzip the contents.
There seem to be a few example on how to do a download in Flex/Air, but not many in Flex/Air/Mobile. Additionally, the mix of examples seem to be all over the place with namespace versions, etc.
Could someone please provide a succinct example on how to do this in an Air Mobile app?
Thank you!
Edit: What I've tried:
So, from the examples on the web, the first thing I did was add namespace in Flex declarations: xmlns:net="flash.net.*"
Then I added the following component: <net:URLLoader id="urlLoader" />
Then, in the button click event, I called it: urlLoader.load(new URLRequest(downloadUrl));
I get a run time error: Error #2044: Unhandled ioError:. text=Error #2032: Stream Error. URL: http://...
The problem is I don't know where to go from here since I'm not sure what's missing.
this code works for me on IOS:
public function download():void{
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(IOErrorEvent.IO_ERROR,function(e:IOErrorEvent):void{
Alert.show('error IO');
});
loader.addEventListener(Event.COMPLETE,downloadComplete);
loader.load(new URLRequest(YOUR_URL));
}
private function downloadComplete(event:Event):void{
try{
var file:File=File.applicationStorageDirectory.resolvePath(YOUR_LOCAL_PATH);
var ba:ByteArray = event.target.data as ByteArray;
var fileStream:FileStream = new FileStream();
fileStream.addEventListener(IOErrorEvent.IO_ERROR,function(e:IOErrorEvent):void{
Alert.show('error IO');
});
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(ba);
fileStream.addEventListener(Event.CLOSE, fileClosed);
fileStream.close();
Alert.show("File downloaded succesfully")
}
catch(eeee){
Alert.show("error")
}
}

Adobe Flex AIR download videos in Android device

I want to download a set of video files to a default folder path in an Android device and then have access to the path and files. The FileReference class does not support this features. Is there another way? Thank you.
I dont use file reference at all. I use a Loader to download the file. I then create a new File object with the path where I want to save the file, and use a FileSteam to write the data from the loader to the file.
var req:URLRequest = new URLRequest(url);
loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, handler);
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.load(req);
function handler(e:Event):void{
var file:File = new File(path);
var stream:FileSteam();
steam.open(file, FileMode.WRITE);
steam.writeBytes(loader.data);
steam.close();
}
something like this should accomplish what you are looking for. I wrote this code from memory so it may not be exactly correct but should be good enough to get you going. Make sure you put this in a try/catch since file and filesteam both throw exceptions

Categories

Resources