Re-loading Swf file from sdcard as3 Android - android

I am working on an android app where I wish to download swf files from an external server, save them to sdcard and load them later in the app. Downloading works fine and the swf is saved in the application directory. Here is my code that loads the swf from the sdcard :
var myloader:Loader = new Loader();
var myhomeButton:btnHome = new btnHome();
addChild(myloader);
var swfFilePath:File = File.applicationStorageDirectory.resolvePath("Android/data/myswffile.swf");
var inFileStream:FileStream = new FileStream();
inFileStream.open(swfFilePath, FileMode.READ);
var swfBytes:ByteArray = new ByteArray();
inFileStream.readBytes(swfBytes);
inFileStream.close();
var loaderContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
loaderContext.allowCodeImport = true;
myloader.contentLoaderInfo.addEventListener(Event.COMPLETE, mycompleteHandler);
myloader.loadBytes(swfBytes, loaderContext);
function mycompleteHandler(evt:Event):void
{
myloader.contentLoaderInfo.removeEventListener(Event.COMPLETE, mycompleteHandler);
addChild(myhomeButton);
myhomeButton.height = _height * 0.08;
myhomeButton.width = myhomeButton.height;
myhomeButton.x = 10;
myhomeButton.y = 10;
myhomeButton.addEventListener(MouseEvent.CLICK, myexitfftn);
}
function myexitftn(evt:Event):void
{
myloader.unloadAndStop(true);
removeChild(myhomeButton);
gotoAndStop(1, "SomeOtherFrame");
}
the problem is when I click the exit button the swf unloads but when I reload it, it starts from the second frame of the loaded swf, the third time from the third frame and so on... Where am I going wrong or what is an alternative solution, please guide.

Try, addChild(myloader) in Complete handler and removeChild and null while exiting, so that loaded swf is completely removed from the memory like so:
function mycompleteHandler(evt:Event):void
{
myloader.contentLoaderInfo.removeEventListener(Event.COMPLETE, mycompleteHandler);
addChild(myloader);
//... Rest of code remains same
}
function myexitftn(evt:Event):void
{
myloader.unloadAndStop();
removeChild(myloader);
myloader = null;
removeChild(myhomeButton);
gotoAndStop(1, "SomeOtherFrame");
}

Related

unload externally loaded swf

In my app, I have loaded some external swf files. It's a game files so for avoiding sandbox error I use the follwing code:
var file:File = File.documentsDirectory.resolvePath("myFilePath");
var toLoadUrl = file.url;
var myUrl:URLRequest;
myUrl = new URLRequest(toLoadUrl);
var inFileStream:FileStream = new FileStream();
inFileStream.open(file, FileMode.READ);
var swfBytes:ByteArray = new ByteArray();
inFileStream.readBytes(swfBytes);
inFileStream.close();
var loaderContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
loaderContext.allowLoadBytesCodeExecution = true;
loaderContext.allowCodeImport = true;
myLoader = new Loader();
myLoader.loadBytes(swfBytes, loaderContext);
//myLoader.load(myUrl, loaderContext);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadComplete_swf);
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loop, false, 0, true);
myLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,loadingError);
And to unload the file, I've used:
if(myLoader)
{
trace("Unloaded");
myLoader.unloadAndStop(true);
myLoader.unload();
}
while(MovieClip(root).holder.numChildren > 0)
{
var item:DisplayObject = MovieClip(root).holder.removeChildAt(0);
trace("Removed");
}
if(contains(myLoader))
{
trace("Remove child");
removeChild(myLoader);
}
It works but only for one time. After that It keeps stacking all the files even the codes to remove it has been executed. I can still hear the sounds and the codes from the swf files are still running. Is there any problem with the above code? How do I completely remove the loaded files? Any help would be appreciated.

adobe flash as3 for android get access file from sdcard

i was made a simple video playback for android application with adobe flash using action script 3, where a flvplayback access playlist from xml file "xmlVideoPlayer.xml", and i put in folder "media/PustakaXML/xmlVideoPlayer.xml" where parent folder same with myfile.fla (project file).
can somebody help me, how to ; if i put myxml file on SDCARD on android mobile. so if i publish it, to APK, and instal on android mobile, the aplication can access myxml file on SDCARD.
below the script on my adobe flash actionscript code.
import fl.video.*;
import flash.events.Event;
import flash.net.*;
// Set Variables
var flvControl:FLVPlayback = display;
var flvIndex:Number = 0;
var loopAtEnd:Boolean = true;
// Load XML file...
var xmlList:XML;
var xmlPath:String = "media/PustakaXML/xmlVideoPlayer.xml";
//HELP HERE if file put in SDCARD
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest(xmlPath));
// Receive the XML and load the first video
function xmlLoadedHandler(event:Event):void
{
// Save XML
xmlList = new XML(xmlLoader.data);
// Set video (Start)
flvControl.source = xmlList.video[0];
}
xmlLoader.addEventListener(Event.COMPLETE, xmlLoadedHandler);
// 3. Handle video completion (load next video)
function completeHandler(event:fl.video.VideoEvent):void
{
// Get next item in list
flvIndex++;
// Validate index
if( flvIndex == xmlList.video.length() ){
if( loopAtEnd ){
flvIndex = 0;
}
else{
return;
}}
// Load next video
flvControl.source = xmlList.video[flvIndex];
}
flvControl.addEventListener(fl.video.VideoEvent.COMPLETE, completeHandler);
i am sorry my English no good enough...

AS3 android camera roll crash and restart

Making an AS3 app for android that uses camera roll to load, select and then use an image.
The cameraroll browser works fine, but when the image is selected, the app crashes - almost every time - as it has worked on a handful of occasions (!?!) We assumed a memory issue and attempted to close all other windows / use smaller photos in the camera roll etc. and try different devices - but cannot recreate success consistently. Cannot find another ANE that works either
It fails at the point where the photo has been selected, and RESTARTS the app...
here's the relevant code, any help appreciated.
public function openGallery():void {
var cameraRoll:CameraRoll = new CameraRoll();
if(CameraRoll.supportsBrowseForImage) {
cameraRoll.addEventListener(MediaEvent.SELECT, imageSelected);
cameraRoll.addEventListener(flash.events.Event.CANCEL, browseCanceled);
cameraRoll.addEventListener(flash.events.ErrorEvent.ERROR, mediaError);
cameraRoll.browseForImage();
}
else { trace( "Image browsing is not supported on this device."); }
}
private function imageSelected(event:MediaEvent):void {
trace("Media selected...");
var imagePromise:MediaPromise = event.data as MediaPromise;
_dataSource = imagePromise.open();
if(imagePromise.isAsync) {
trace("Asynchronous media promise.");
var eventSource:IEventDispatcher = _dataSource as IEventDispatcher;
eventSource.addEventListener(flash.events.Event.COMPLETE, onMediaLoaded);
} else {
trace("Synchronous media promise.");
readMediaData();
}
}
private function onMediaLoaded(event:flash.events.Event):void{
trace("Media load complete");
_mediaBytes = new ByteArray();
_dataSource.readBytes(_mediaBytes);
_tempDir = File.createTempDirectory();
var now:Date = new Date();
var filename:String;
filename = now.fullYear + now.month + now.day+now.hours + now.minutes + now.seconds + ".JPG";
_file = _tempDir.resolvePath(filename);
//writing temporal file to display image
_stream = new FileStream();
_stream.open(_file,FileMode.WRITE);
_stream.writeBytes(_mediaBytes);
_stream.close();
if(_file.exists){
_imageLoader = new Loader();
_imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,onMediaLoadedBitmapData);
_imageLoader.loadBytes(_mediaBytes);
}
}
private function onMediaLoadedBitmapData(event:Event):void{
trace("onMediaLoadedBitmapData");
var loaderInfo:LoaderInfo = LoaderInfo(event.target);
_bitmapData = new BitmapData(loaderInfo.width,loaderInfo.height,false,0xFFFFFF);
_bitmapData.draw(loaderInfo.loader);
addPictureToScreen();
}
I had a similar thing happen in Air for iOS but it was related to picking large images. I just checked the dimensions of the chosen image and then used a scalar matrix if they were past a certain point. Sounds like you've thought about this but maybe look further into that?

Loading External Picture in air for android

I need load images from a folder in application directory created in sd Card of mobile android
This is working in PC, but not in mobile version
Some solution ?
var imagesFin:File = File.applicationStorageDirectory.resolvePath("appImages/");
var files:Array = imagesFin.getDirectoryListing();
for (var i:uint = 0; i < files.length; i++){
var picLoader = new Loader();
picLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadPicture);
picLoader.load(new URLRequest(files[i].nativePath));
}
function loadPicture(event:Event):void {
var imgAtual = event.target.content;
ContainerImagesList.addChild(imgAtual);
}
This work for me:
var bytes:ByteArray = new ByteArray();
var myFileStream:FileStream = new FileStream();
var myFile:File = File.documentsDirectory.resolvePath(YOUR_PATH);
myFileStream.addEventListener(Event.COMPLETE, fileCompleteHandler)
myFileStream.openAsync(myFile, FileMode.READ);
function fileCompleteHandler(event:Event):void
{
myFileStream.readBytes(bytes);
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
loader.loadBytes(bytes);
function imageLoaded(e:Event):void
{
addChild(Bitmap(loader.content));
myFileStream.removeEventListener(Event.COMPLETE, fileCompleteHandler);
myFileStream.close();
}
}
Cheers!
In iOS I never use resolvePath. I just use a relative url like "images/photo.jpg" and it works perfectly on any iOS device.
Instead of resolvePath you could also use URL schemes. It's a lot simpler "app:/images/photo.jpg" or "app-storage:/settings/prefs.xml".
http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118666ade46-7fe4.html
This is what I do to get all images in my image file cache
var imageFolder:File = new File("app:/img");
var getfiles:Array = imageFolder.getDirectoryListing();

Issue accessing local xml files in apk file - Air for Android

This is my first time really getting my teeth into Air for Android so please forgive me if this issue has been covered already. If it has then I've been unable to find it.
So I have an application that loads and displays xml data.
In the app I've got code to check if wiFi or equivalent is available and if so then pull live xml file and if not then pull the local xml file that was packaged with the application.
The app works fine if I am pulling in the xml from the live url but not if pulling from local.
After doing some research I discovered that when pulling in local file then Air for Android works slightly differently. So I need to resolve the application directory.
I did this and still no joy.
After a bit more research I read some post's that said I should use fileStream()
Tried this and still nada :(
All the time whilst testing in Flash IDE it works as intended.
If I had any more hair left I would be pulling it out right now!
The local xml file is set in the "includes"
Sample code below I am using for testing
var subURL:String = "xml_feeds/myxmlfile.xml"
var fileStream:FileStream = new FileStream();
var file:File = File.applicationDirectory.resolvePath(subURL);
fileStream.addEventListener(Event.COMPLETE, processXMLData);
fileStream.openAsync(file, FileMode.READ);
MovieClip(parent).txStates.text = file.url+" - TRYING"
var prefsXML:XML = new XML()
function processXMLData(event:Event):void{
MovieClip(parent).txStates.text = file.url+" - OPEN"
prefsXML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
var tempArr:Array = new Array();
var reportCount:Number = prefsXML.row.column.length()
for (var i = 0; i < reportCount; i++) {
var rName:String = prefsXML.row.column[i].#name.toString();
var rValue:String = prefsXML.row.column[i].toString();
var rTitle:String = prefsXML.row.column[i].#name.toString()
tempArr.push([rName, rValue, rTitle]);
}
showData()
fileStream.close();
}
Is there anything I've missed?
UPDATE: 21/08/12
No idea what is going on with this. Here is the code i now have to use in order to load in the local xml file. Seems rather long winded
function listing():void{
var folders:Array = new Array();
folders = File.applicationDirectory.getDirectoryListing();
for(var i:Number =0;i<folders.length;i++){
if(folders[i].isDirectory){
if(folders[i].name=="xml_feeds"){
var files:Array = new Array();
files = folders[i].getDirectoryListing();
for(var j:Number=0;j<files.length;j++){
if(files[j].name=="CTSection2.xml"){
fileStream.openAsync(files[j], FileMode.READ);
fileStream.addEventListener(Event.COMPLETE, processXMLData);
fileStream.addEventListener(IOErrorEvent.IO_ERROR, localXMLFailLoad);
}
}
}
}
}
}

Categories

Resources