Adobe AIR and different OS filesystems - actionscript-3

Another Adobe Air question for you but first here some background into the project I have been tasked with. It is an AIR app that will read assets from a USB key and must work on both WIN and MacOS. The problem is, how do I load assets into the app on MacOS! Sounds simple enough and works seamlessly on Windows.
Here is a code snippet of what i am trying to do:
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, ok);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
var p:String;
if (os == "mac")
{
p = "/Volumes/" + keyVolume.rootDirectory.name + File.separator + "0a0ff0ff-f7ae-4b9c-9637-843b1d6c80e8.jpg";
}
else
{
p = keyVolume.rootDirectory.name + File.separator + "0a0ff0ff-f7ae-4b9c-9637-843b1d6c80e8.jpg";
}
var temp:File = new File(p);
Debugger.Display.text += "\nAttempting to load: " + p;
Debugger.Display.text += "\nDoes it exist? " + temp.exists;
loader.load(new URLRequest(p));
... the variable OS and keyVolume are being successfully set in earlier code. Also, I have the event listener callbacks defined as well for ok() and ioErro().
When this is run it prints out on windows:
Attempting to load: G:\0a0ff0ff-f7ae-4b9c-9637-843b1d6c80e8.jpg
Does it exist: true
... and then successfully loads the asset.
On MacOS, it prints out:
Attempting to load: /Volumes/AC/0a0ff0ff-f7ae-4b9c-9637-843b1d6c80e8.jpg
Does it exist: true
... and then fails with an IOError every time.
Can anyone see something that I am missing here? Do I have some sort of permission error or something (file has "read / write" access). The USB key is formatted in MS-DOS FAT32, could that be a problem?
EDIT
I formatted a new USB key in MacOS to FAT16 and put the files onto it with no success. Problems remain.
EDIT
I am now just trying to load an asset from /users/-USERNAME-/Desktop and still am receiving the same error, so it looks like it isn't a permissions issue on just the USB stick, it is more widespread than that.
EDIT
PROBLEM SOLVED! I finally worded my Google search correctly and it revealed the answer.
These changes will fix the problem:
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, ok);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
var p:String = keyVolume.rootDirectory.nativePath + ((os == "mac") ? File.separator : "") + "0a0ff0ff-f7ae-4b9c-9637-843b1d6c80e8.jpg";
var temp:File = new File(p);
Debugger.Display.text += "\nAttempting to load: " + temp.url;
Debugger.Display.text += "\nDoes it exist? " + temp.exists;
loader.load(new URLRequest(temp.url));
I have also refined the logical statement involving the OS detection a bit as well.
I hope someone finds this useful!

PROBLEM SOLVED! I finally worded my Google search correctly and it revealed the answer.
These changes will fix the problem:
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, ok);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
var p:String = keyVolume.rootDirectory.nativePath + ((os == "mac") ? File.separator : "") + "0a0ff0ff-f7ae-4b9c-9637-843b1d6c80e8.jpg";
var temp:File = new File(p);
Debugger.Display.text += "\nAttempting to load: " + temp.url;
Debugger.Display.text += "\nDoes it exist? " + temp.exists;
loader.load(new URLRequest(temp.url));
I have also refined the logical statement involving the OS detection a bit as well.
I hope someone finds this useful!

Related

open local html file with navigateToURL on android & ios

I have created an HTML file and I want to show it with navigateToUrl method:
var htmlFile:File = File.documentsDirectory.resolvePath("WebWin//Factor//" + "Factor" + ".html");
var fs:FileStream = new FileStream();
fs.open(htmlFile, FileMode.WRITE);
fs.writeUTFBytes(htmlString);
fs.close();
if(htmlFile.exists)
{
navigateToURL(new URLRequest(htmlFile.url), "_blank");
}
but on Android or iOS I can't open HTML file (I don't have any issue on windows)
this is htmlFile.url :
file///var/mobile/Containers/Data/Application/AB199919-1E41-47E7-9716-DA5CD746D113/Documents/WebWin/Factor/Factor.html
Also for URLRequet url, I tried other ways like("http://"+htmlFile.url) but I wasn't successful.
Do me a big favor if you help me

Animate 2021 Action Script 3 URLRequest causes Error 2035

I'm working on an application and we are using Flash to add an interface. During the initialize the app loads in a config file with references to image paths. When I use the Loader and URLRequest classes to load those images the path is not working and it's causing issues. Based on everything I've researched my code should work. I have a trace that outputs "source/icons/default.png" which is the correct relative path for the external image and loading the image works, but I need to use a variable to set the image path. Setting the URLRequest with a variable, new URLRequest("source/icons/" + default.png);, causes it use a full file path that looks wrong and reports a 2035 error. Is there something that I'm doing wrong? I'm not sure what to do at this point. I've verified the file exists and I've verified the path of the file.
Trace Ouput:
source/icons/default.png
Error Output:
[CompanionStatus] Error occurred while loading bitmap
[CompanionStatus] 2035 : Error #2035: URL Not Found. URL: file:///C|/Users/devtrooper/Desktop/project/source/icons/default.png
It looks like flash is replacing the colon (:) near the drive letter with a pipe (|) and I'm not sure if that is the issue.
Here is my method for loading images:
private function getBitmapData(nameString:String, imagePath:String):void{
try{
var bitmapDataFunction:Function = addBitmapDataToDictionary(nameString);
var path:String = ICON_FOLDER_PATH + imagePath;
var loader:Loader = new Loader();
var urlRequest:URLRequest = new URLRequest("source/icons/" + imagePath);
trace("URLRequest.url " + urlRequest.url);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, bitmapDataFunction, false, 0, true);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, handleBitmapDataError, false, 0, true);
loader.load(urlRequest);
} catch (e:Error) {
log("Error occured while trying to load image data");
log(e.name + " : " + e.message);
}
}
you can use File method instead of urlRequest method like this:
var urlPath:String="";
var picFile:File = File.desktopDirectory.resolvePath("source/icons/default.png");//you can use applicationDirectory,applicationStorageDirectory,userDirectory,doumenntDirectory and ...
if (picFile.exists == true) {
urlPath = picFile.url;
}

how to get a function to activate which is ment to be used with

I have this code were I take from a external XML file a Link of an Image load it with ...
<mx:Label
id="textboxLink" text=""/>
private function loadRemoteImg(url:String):void {
textboxLink.text
.....
loader, completeHandler etc.
Save Image(), with ByteArray, JPEGEcoder and then to the location - filestream etc.
This works all fine yet it is only possible (due to supposedly Flash Player 10 onwards) by MouseEvent so of a Click of a button!
As mentioned it works all fine, but I really would need this to activate on start up like in a creationComplete or else!
Any help or any ideas would be appriciated! regards aktell
Ah, sorry, I thought you were just trying to load the image; I didn't see that you were trying to save it as well.
For both of the following cases, you'll need to load the image as Binary:
var urlLoader:URLLoader = new URLLoader(new URLRequest( myURL ));
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
...
This is because if we load it normally (using a Loader), then what we'll get is a Bitmap object, i.e. Flash's representation of your image, rather than the jpg/png data itself. Loading it using this method will give you a ByteArray when it's loaded.
If you're using AIR, you should be able to use the FileStream class: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/FileStream.html
Something like:
// NOTE: you can use any of the other File constants to get your path (e.g.
// documentsDirectory, desktopDirectory...
// myImageFileName is the name of your image, e.g. "myPic.jpg"
var file:File = File.applicationStorageDirectory.resolvePath( myImageFileName );
var fs:FileStream = new FileStream;
try
{
fs.open( file, FileMode.WRITE );
fs.writeBytes( imageBinaryData ); // imageBinaryData is a ByteArray
fs.close();
}
catch ( e:Error )
{
trace( "Can't save image: " + e.errorID + ": " + e.name + ": " + e.message );
}
If you're using Flash, then the only way you can save something without user interaction, is through a SharedObject. This will mean that you data won't be available outside the app (it'll be a .sol file), but depending on how you're doing it, this might not be a problem.
// get our shared object - if you're saving a lot of images, then you might need another shared
// object, whose name you know, that stores the names of the other images
var so:SharedObject = null;
try { so = SharedObject.getLocal( this.m_name ); }
catch ( e:Error )
{
trace( "Couldn't get shared object: " + e.errorID + ": " + e.name + ": " + e.message );
return;
}
// NOTE: it's possible you may need a registerClassAlias on the ByteArray before this
so.data["image"] = imageBinaryData;
// save the lso
try { so.flush( minDiskSpace ); }
catch ( e:Error )
{
trace( "Couldn't save the lso: " + e.errorID + ": " + e.name + ": " + e.message );
}
To actually use your image later, load your file(in binary mode)/get your SharedObject, and convert the saved binary to an image:
var l:Loader = new Loader;
l.contentLoaderInfo.addEventListener( Event.COMPLETE, this._onConvertComplete ); // you could probably also listen for the IO_ERROR event
try
{
// NOTE: you can pass a LoaderContext to the loadBytes methods
l.loadBytes( imageBinaryData );
}
catch( e:Error )
{
trace( "Couldn't convert image: " + e.errorID + ": " + e.name + ": " + e.message );
}
...
// called when our loader has finished converting our image
private function _onConvertComplete( e:Event ):void
{
// remove the event listeners
var loaderInfo:LoaderInfo = ( e.target as LoaderInfo );
loaderInfo.removeEventListener( Event.COMPLETE, this._onConvertComplete );
// get our image
var bitmap:Bitmap = loaderInfo.content;
this.addChild( bitmap );
}
If you can't use any of those methods, then you're going to have to have some sort of user interaction (e.g. mouse click). (Out of curiosity, have you tried just dispatching a MouseEvent.CLICK on the relevant object, to see if it would work?)

uploading swf file to stencyl issue?

Alright so I am using the stencyl engine to make a flash game, and I wanted to upload an Swf file. The problem is I keep getting an error
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at _247gamesonline_fla::bn0_Symbol1_2/frame1()
Now I went to that part of the fla file that had the error, and tried to see what was wrong, but could not figure it out. Here is the code:
stop();
var doMain:String = this.stage.loaderInfo.url;
var doMainArray:Array = doMain.split("/");
var domainname = doMainArray[2];
var gamename = "nameofgame";
var linktodjp = "http://www.247gamesonline.com/? p=cat&order=2&tag=puzzle&utm_medium=partnergame&utm_campaign=" + gamename + "&utm_source=" + domainname + "&utm_content=partnergame";
var linktodjpfb = "http://www.facebook.com/connect/uiserver.php?app_id=201345053230176&next=http://www.3j.com/processfacebook.php%3Futm_source%3D" + domainname + "%26fgamet%3D76&display=page&cancel_url=http%3A%2F%2Fwww.3j.com%3F3Futm_source%3D" + domainname + "&locale=en_US&return_session=1&session_version=3&fbconnect=1&canvas=0&legacy_return=1&method=permissions.request&perms=publish_stream,offline_access,email,user_birthday,user_interests,read_friendlists";
buttonplay.addEventListener(MouseEvent.CLICK, playlogonow);
logo.addEventListener(MouseEvent.CLICK, gotowebsite);
logo.buttonMode = true;
stop();
function playlogonow(e:Event) {
logo.play();
gotoAndStop(2);
return;
}
function gotowebsite(e:Event) {
navigateToURL(new URLRequest(linktodjp));
return;
}
Also in another part of the code it simply says
on (release) {
_root.play();
}
Help anyone?
Difficult to say by having that code only. Whatever, I would recommend to put many trace statements there to figure out what variable actually equals null and leads to that exception. Then act accordingly.

I am trying to load a facebook profile picture with Loader()

I'm having trouble passing the url for a users facebook profile picture to a Loader() variable. I'm using a PHP file to get around the security and from the debug I made, it shows that I'm getting the URL fine, but the Loader runs the error event listener. Here is what my facebookProxy.php file looks like;
<?php
$path=$_GET['path'];
header("Content-Description: Facebook Proxied File");
header("Content-Type: image");
header("Content-Disposition: attactment; filename =".$path);
#readfile($path);
?>
and here is my code for loading the url then loading the image.
function LoadMyPicture():void
{
debug.text = "Entered loadMyPicture() function";
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, MyPictureLoaded, false, 0, true);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, MyPictureLoadError, false, 0, true);
var request:URLRequest = new URLRequest("facebookProxy.php");
var variables:URLVariables = new URLVariables();
variables.path = Facebook.getImageUrl(Facebook.getSession().uid);
request.data = variables;
loader.load(request);
addChild(loader);
debug.appendText(" \nURLRequest url: " + request.url);
debug.appendText(" \nURLRequest data: " + request.data);
debug.appendText(" \n" + loader.content);
}
function MyPictureLoaded(e:Event):void
{
debug.appendText(" \nEntering MyPictureLoaded");
var loader:Loader = e.target.loader;
var bitmap = Bitmap(loader.content);
bitmap.smoothing = true;
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, MyPictureLoaded);
loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, MyPictureLoadError);
debug.appendText(" \nLoader Content: " + loader.content);
//Note: picturePlaceholder is just a blank movie clip on the Stage
bitmap.width = picturePlaceHolder.width;
bitmap.height = picturePlaceHolder.height;
picturePlaceHolder.addChild(bitmap);
debug.appendText(" \nBitmap width: " + String(bitmap.width) +
" \nBitmap height: " + String(bitmap.height))
}
function MyPictureLoadError(e:Event):void
{
debug.appendText(" \nMyPictureLoadError: Loader Error!");
}
debug.appendText(" \nURLRequest url: " + request.url);
debug.appendText(" \nURLRequest data: " + request.data);
debug.appendText(" \n" + loader.content);
These lines show as follows;
URLRequest url: facebookProxy.php
URLRequest data: path=https%3A%2F%2Fgraph%2Efacebook%2Ecom%2F100001902730917%2Fpicture
null
So the content in the loader is null, how would I debug this? Does anyone have any solutions that could help me?
EDIT
I forgot to mention that I followed a tutorial to that did not explain any of the basics about the code that they provided, so I'm fairly lost to the concept of the loader. This is the tutorial I followed Loading Facebook profile picture into Flash SWF using open graph api.
The line
debug.appendText(" \n" + loader.content);
occurs outside of the Event.COMPLETE handler.
I would add more listeners for the open, progress, and httpStatus events. The httpStatus event should be very helpful.
Can you add some debugging in your facebookProxy.php?
I was able to figure this out. The variables.path and request.data together with the facebookProxy.php wasn't necessary at all.
After changing the
var request:URLRequest = new URLRequest("facebookProxy.php");
to
var request:URLRequest = new URLRequest(Facebook.getImageUrl(Facebook.getSession().uid, large));
loader.load(request);
picturePlaceHolder.addChild(loader);
Removing
var variables:URLVariables = new URLVariables();
variables.path = Facebook.getImageUrl(Facebook.getSession().uid);
request.data = variables;
Allowed the picture to load properly. It's hard to find an up to date tutorial for all of this, why is that? but I guess I would have to open up a new question to find out lol.