How to upload a background image using actionscript 3.0 code? - actionscript-3

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.

Related

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]:

AS3 can not load a Bitmap by using loadByte in Thread?

Flash Player supported Thread in 11.5+.
I want to load an image by using Loader.loadBytes() in Worker Thread.
Which the Image ByteArray are generated in Main Thread.
But I can NOT do it.
I got a SecurityError like this:
SecurityError: Error #2123: Security sandbox violation:
Loader.content: file:///E:/work/ASWorkSpace/test/bin-debug/test.swf
cannot access
file:///E:/work/ASWorkSpace/test/bin-debug/test.swf/[[DYNAMIC]]/1. No
policy files granted access.
I init my worker thread like:
worker = WorkerDomain.current.createWorker(this.loaderInfo.bytes);
so it's not a swf from remote, but it's local or like
[Embed(source="../workerswfs/Thread.swf", mimeType="application/octet-stream")]
I found in manual.
It said "If the loaded content is an image, its data cannot be accessed by a SWF file outside of the security sandbox, unless the domain of that SWF file was included in a URL policy file at the origin domain of the image."
I have gotten a solution. I loadByte() twice, it seems "washed off" the ByteArray source. so FlashPlayer considered the ByteArray generated by Worker Thread, and it allowed to access loader.content. Like this:
//Messages to the Main thread
protected function onMainToWorker(event:Event):void {
var msg:ByteArray = mainToWorker.receive() as ByteArray;
trace(msg.length);
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, bytesComplete);
var bytes:ByteArray = new ByteArray();
bytes.writeBytes(msg,0,msg.length);
loader.loadBytes(bytes);
}
private function loadedAagin(e:Event):void
{
var loader:Loader = (e.target as LoaderInfo).loader;
// Here we can access it.
var bmp:Bitmap = loader.content as Bitmap;
}
private function bytesComplete(e:Event):void
{
var loader:Loader = (e.target as LoaderInfo).loader;
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, bytesComplete);
// var bmp:Bitmap = loader.content as Bitmap; It can not be accessed
// just loadBytes again, it seems "washed off" the ByteArray source
var newloader:Loader = new Loader();
newloader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedAagin);
newloader.loadBytes(loader.contentLoaderInfo.bytes);
}
But it's so ugly isn't it?
Is there anyone has good idea?

Can't load image in swf

I get error with load image into swf file. Here is my code:
private var loader:Loader = new Loader( );
public function Battle()
{
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest("http://www.google.com.vn/images/srpr/logo4w.png"));
}
private function onComplete(event:Event):void
{
addChild(loader);
}
e very thing is OK, logo of Google appeared. But I need to manipulate pixel the image have loaded. I add a image Bitmap :
private var loader:Loader = new Loader( );
private var image:Bitmap;
public function Battle()
{
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest("http://www.google.com.vn/images/srpr/logo4w.png"));
}
private function onComplete(event:Event):void
{
image = Bitmap(loader.content);
this.addChild(image);
}
There is nothing appear. I can't find any solution on internet. I try and detect :
private function onComplete(event:Event):void
{
image = Bitmap(loader.content);
//after above line, rest of function never run
addChild(image); // <-- no run ##
trace("this no run"); // <-- no run ##
}
I use Flash Builder 4 and have no error or any warning.
Can somebody show me a solution, please?
Thank for reading.
You can't access pixel data of an image if image is in different domain than your application (and there is no crossdomain policy file)
1 You need crossdomain.xml on Google site :)
2 You need use checkpolicy:
var context:LoaderContext = new LoaderContext();
context.checkPolicyFile = true;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest("http://www.google.com.vn/images/srpr/logo4w.png"), context);
but it works only if Google add crossdomain.
3 You can use little serverside proxy on your domain. Flash call proxy to get file with your url, proxy download it and return to flash...

Image path issue on as3

I'm trying to create a screensaver for one our of our display we have at work. Images will be uploaded to an external server, from that server I will have pull the images and xml file. so my flash app and my content will be in two different places. I'm getting an error "SecurityError: Error #2000: No active security context". how do I override error and get the images to my stage.
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML;
var imageList:XMLList;
var imageLoader:Loader = new Loader();
var timer:Timer =new Timer(5000);
var imageIndex:uint = 0;
var child:DisplayObject;
var path:String="http://bgxserv03.mgmmirage.org/interactivemedia/mmhub01/test/mb/edit_bay/hr/infoscreen/servamb/";
xmlLoader.load(new URLRequest(path +"output.xml"));
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
timer.addEventListener(TimerEvent.TIMER, tick);
function xmlLoaded(e:Event) {
xmlData = new XML ( e.target.data);
imageList = xmlData.image.name;
timer.start();
loadImage(imageList[0]);
}
function imageLoaded(e:Event){
if (child){
myImageHolder.removeChild(child);
}
child = myImageHolder.addChild(imageLoader);
Tweener.addTween(child, {alpha:0, time:1, delay:4});
Tweener.addTween(child, {alpha:1, time:1, delay:5});
}
function loadImage(path:String){
imageLoader.load(new URLRequest( path +"photos/"));
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,imageLoaded);
}
Any help would be deeply appreciate. Thank you.
You need to put the "Crossdomain.XML" on the server's root directory. This will allow your flash file to access the data (image in your case) from that server. You can get a sample xml from the following URL, customize it for your server:
Sample CrossDomain.XML
What you are missing is probably a crossdomain.xml policy file at the domain of your image/xml files.
Use this link to create a crossdomain.xml file and add it to the root of your image/xml domain like so : "http://bgxserv03.mgmmirage.org/crossdomain.xml"
The URLLoader load() function automatically checks for the crossdomain.xml. Loader class requires you specify that you are interested in checking for a policy file in a LoaderContext object sent to the load() function.
In your code, it looks like the error should be coming from the URLLoader xml file request, since it doesn't look like you are trying to access the bitmap data of your images in any way, which is normally what would throw a security error for image files. If it is a problem with the image loading part, then complete the following instructions and you should be set to go:
In your loadImage function, add a LoaderContext parameter to your load method call:
function loadImage(path:String){
var loaderContext:LoaderContext = new LoaderContext();
loaderContext.checkPolicyFile = true;
imageLoader.load(new URLRequest( path +"photos/"), loaderContext);
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,imageLoaded);
}
Check out the spec for more info on how to use the Loader class.
If you run into any trouble, this thread may be helpful.

execute remote SWF in AIR Security Sandbox

I try to download an external SWF and run it within the AIR security sandbox.
this is the code of the AIR application:
public class Downloader extends Sprite
{
private static const REMOTE_FILE:URLRequest = new URLRequest("http://myserver.com/downloadable.swf");
private var _main:NativeWindow;
public function Downloader()
{
var loader:URLLoader = new URLLoader(REMOTE_FILE);
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, downloadComplete);
}
private function downloadComplete(e:Event):void{
var ba:ByteArray = e.target.data;
var stream:FileStream = new FileStream();
var file:File = File.applicationStorageDirectory.resolvePath("downloadable.swf");
stream.open(file, FileMode.WRITE);
stream.writeBytes(ba);
stream.close();
loadAndRunSwf();
}
private function loadAndRunSwf(){
this._main = new NativeWindow();
this._main.width = 1024;
this._main.height = 768;
////obsolete?
//var context:LoaderContext = new LoaderContext();
//context.allowLoadBytesCodeExecution = true;
//context.applicationDomain = ApplicationDomain.currentDomain;
var file:File = File.applicationStorageDirectory.resolvePath("downloadable.swf");
var loader:Loader = new Loader();
loader.load(new URLRequest(file.url)/*,context*/);
this._main.stage.addChild(loader);
this._main.activate();
}
}
Code of the downloadable.swf:
public class Downloadable extends Sprite
{
private var _btn:Button = new Button();
private var _baseFolder:File = new File("app-storage:/");
public function downloadable_test()
{
this.addChild(_btn);
_btn.label = "access Harddisk";
...
}
}
so now, If I run Downloader, it will download the swf and try to run it, but i'll get an exception in Downloadable on the line
private var _baseFolder:File = new File("app-storage:/");
the error:
SecurityError: file
at runtime::SecurityManager$/checkPrivilegeForCaller()
So - what Do I need to do to prevent such security errors? I want my remote SWF to be treated as native code running in the same security sandbox as the AIR code.
I'm not sure about Android, but for the regular web player you would need to specify SecurityDomain.currentDomain for the securityDomain of the Loader's context for the loaded code to be considered equal to the loader in terms of permissions. Also note that for reasons impossible to explain, if you use SecurityDomain when loading from the file system on PC Flash Player will complain.
However complicated, the Flash Player security is often a security by obscurity... So, if it doesn't work the way you coded it, try Loader.loadBytes() "workaround".
function loadAndRunSwf()
{
var context:LoaderContext=new LoaderContext(false);
context.allowCodeImport=true;
var ba:ByteArray=new ByteArray();
var file:File = File.applicationStorageDirectory.resolvePath("downloadable.swf");
var fileStream:FileStream=new FileStream();
var loader:Loader = new Loader();
fileStream.open(file,"read");
fileStream.readBytes(ba);
ba.position=0;
loader.loadBytes(ba,context);
this._main = new NativeWindow();
this._main.width = 1024;
this._main.height = 768;
this._main.stage.addChild(loader);
this._main.activate();
}