AS3 Try Catch Url Loader - actionscript-3

I am trying to load swf from an url. there isnt an error but i am trying to give error if internet connection was lost. I used this code. it loads the url but not give an error.where is my mistake can you help me?
var myLoader:Loader = new Loader();
try {
var url:URLRequest = new URLRequest("http://....swf");
myLoader.load(url);
addChild(myLoader);
}
catch(error:Error){
trace("Error loading image thumbnail");
}

try-catch doesn't work for loading resources. As loading a resource is an asynchronous process the error is not thrown immediately, so try-catch is not able to catch the error. In this case, error handling is done using event listeners as show below:
var myLoader:Loader = new Loader();
myLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loaderIOErrorHandler);
var url:URLRequest = new URLRequest("http://....swf");
myLoader.load(url);
addChild(myLoader);
function loaderIOErrorHandler(e:Event){
trace("Error loading image thumbnail");
}

Related

ActionScript 3.0 can't load SWF file

I have a problem about loading a SWF file(the SWF with a Main.as)
Here is my code:
bt1.addEventListener(MouseEvent.CLICK, f1);
var myloader:Loader = new Loader();
var myURL:URLRequest = new URLRequest("test.swf");
myloader.load(myURL);
function f1(Event:MouseEvent):void
{
addChild(myloader);
myloader.x=100;
myloader.y=200;
myloader.width =200;
myloader.height =60;
}
When I clicked load button, I can hear the sounds(test.swf), but I cant see anything.
Did you try with stage.addChild(myloader);?

How do I make sure a file opened by URLLoader is closed so I can write on it with a FileStrewam

Need to load a file change it and then write the changes back into it. I get a File in use error. How do I make sure the file is closed before I can write on it:
protected function getData():void
{
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, onComplete);
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.load(new URLRequest(filePath));
}
protected function onComplete(e:Event):void
{
var data:XML = new XML((e.target as URLLoader).data);
data.replace('User',<User></User>);
var stream:FileStream = new FileStream();
stream.open(new File(filePath),FileMode.WRITE);
stream.writeMultiByte('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n<?xml-stylesheet type="text/xsl" href="template.xsl"?>\n'+data.toXMLString(),File.systemCharset);
stream.close();
}
I get the error Error #2044: Unhandled IOErrorEvent:. text=Error #3013: File or directory is in use.
at views::JobsManagement/readyToClean()[C:\Users\David\Adobe Flash Builder 4.7\NxMobileInspect_FixingMemoryIssues\src\views\JobsManagement.mxml:190]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
Already tried opening with method openAsync did not work. Error comes as a error window instead of coming from debugger.
Any ideas?
You can try using urlLoader.close() but personally I use a try catch and then a Timer dealy until the file is available for writing. You can of course limit the number of try catch used. Something like:
protected function onComplete(e:Event):void
{
var data:XML = new XML((e.target as URLLoader).data);
data.replace('User',<User></User>);
saveFile();
}
private function saveFile(e:Event = null):void
{
try
{
//FileStream write
}
catch(er:Error)
{
//set a timer with listener saveFile and try again
}
}

AS3 URLRequest not working in browser

My URL request is working perfectly in Flash Pro, but when I test it in the Browser, the image doesn't work at all. It doesn't load. Is there a Publish Setting I need to be using or something?
Please help, it's pretty frustrating. Here's what it looks like when it's in the browser.
Relevant code:
public function startSlidingPuzzle() {
// blank spot is the bottom right
blankPoint = new Point(numPiecesHoriz-1,numPiecesVert-1);
// load the bitmap
loadBitmap("http://fc07.deviantart.net/fs71/f/2014/030/d/7/slidingimage_by_nooodisaster-d74et6t.jpg");
}
// get the bitmap from an external source
public function loadBitmap(bitmapFile:String) {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingDone);
var request:URLRequest = new URLRequest(bitmapFile);
loader.load(request);
}
// bitmap done loading, cut into pieces
public function loadingDone(event:Event):void {
// create new image to hold loaded bitmap
var image:Bitmap = Bitmap(event.target.loader.content);
pieceWidth = image.width/numPiecesHoriz;
pieceHeight = image.height/numPiecesVert;
trace(numPiecesHoriz)
// cut into puzzle pieces
makePuzzlePieces(image.bitmapData);
// shuffle them
shufflePuzzlePieces();
}
You have a security error:
SecurityError: Error #2122: Security sandbox violation: Loader.content: http://fc03.deviantart.net/fs71/f/2014/030/e/e/beyonce_slidingpuzzle___flash_diary_day_10_by_nooodisaster-d74er8h.swf cannot access http://i.stack.imgur.com/ls9QI.jpg. A policy file is required, but the checkPolicyFile flag was not set when this media was loaded.
at flash.display::Loader/get content()
at SlidingPuzzle/loadingDone()
Try adding a security context:
public function loadBitmap(bitmapFile:String)
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingDone);
var context:LoaderContext = new LoaderContext();
context.applicationDomain = ApplicationDomain.currentDomain;
context.securityDomain = SecurityDomain.currentDomain;
context.checkPolicyFile = true;
var request:URLRequest = new URLRequest( bitmapFile );
loader.load( request, context );
}
If that doesn't fix it you'll need to add a crossdomain.xml to the server hosting the images you are requesting. Why not save the images locally and deploy them in the same scope as your build?
[1]:

How to upload a background image using actionscript 3.0 code?

Need your help. I am trying it first time.
I have used following code.
But i get this in my console:
started loading file
SecurityError: Error #2000: No active security context.
and my image url is in same folder as my script file.
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, fileLoaded);
loader.load(new URLRequest("C:\Documents and Settings\Owner\Desktop\23Aug\demo1\mic.jpg"), context);
var context:LoaderContext = new LoaderContext();
context.applicationDomain = ApplicationDomain.currentDomain;
trace("started loading file");
addChild(loader);
function fileLoaded(event:Event):void
{
trace("file loaded");
}
Reasons to throw securityError exception.
Invalid path,
Trying to access a URL, that not permitted by the security sandbox,
Trying a socket connection, that exceeding the port limit, and
Trying to access a device, that has been denied by the user(Ex., camera, microphone.)
try this
private var _loader:Loader = new Loader();
private var _context:LoaderContext = new LoaderContext();
private var _url:URLRequest = new URLRequest("demo1/mic.jpg");
_context.checkPolicyFile = false;
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageloaded);
//_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
_loader.load(_url, _context);
private function onImageloaded(e:Event):void{
addChild(e.target.content);
}
Use slashes instead of back slashes :
loader.load(new URLRequest("C:/Documents and Settings/Owner/Desktop/23Aug/demo1\mic.jpg"), context);
But the best way is to use relative path like "./mic.jpg" and do not use absolute path.

How to load image in Action Script?

How to load image in Action Script ?
i have used the following code but image was not loaded.`var ldr:Loader = new Loader();
var url:String = "D:\BlackBerry\workspace\SoundTest\blackberry-tablet-icon.png.bmp";
var urlReq:URLRequest = new URLRequest(url);
ldr.load(urlReq);
addChild(ldr);
stage.nativeWindow.visible = true;`
Please help ?
You need to check the errors that this loader gives:
ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
ldr.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, onStatus);
function onError(e:IOErrorEvent):void{
trace(e);
}
function onStatus(e:HTTPStatusEvent):void{
trace(e);
}
Now you can filter the errors out and search on the web the specific arrow this loader gives.
I guess its a security error. Flash prevents to load images outside his domain(exception is debug version in Flash cs5)
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/SecurityError.html