« Some Adobe Flex/Flash related resources from India | Main | Apollo - I am thinking about it »

January 28, 2007

Enabling BitmapData.draw (..) on crossdomain images in Adobe Flash Player 9

Adobe Flash Player 8 and 9 have security-sandbox where-in you can not use BitmapData's draw (..) on crossdomain images, as I mentioned in one of my posts. There is no easy way of doing it with Flash Player 8, you need some kind of shim/library.swf or server-side proxy.

Fortunately, Adobe Flash Player 9 has some APIs (Loader, LoaderContext) to make life easier, provided you have a crossdomain.xml with right permission on the server hosting images.

Say "ServerA" has main application and "ServerB" hosts image files. You need to place crossdomain.xml in root of "ServerB". Just having crossdomain.xml is not sufficient, so you need to do something more:-

  1. Create a LoaderContext instance
  2. Set it's checkPolicyFile = true;
  3. Pass LoaderContext instance to Loader.load (..) method, as shown below.

Note: Following code shows the steps, it's not a working example.


var request:URLRequest = new URLRequest ();
request.url = "http://ServerB/images/foo.jpg";

//This is important step..
var loaderContext:LoaderContext = new LoaderContext ();
loaderContext.checkPolicyFile = true;

var loader:Loader = new Loader ();
loader.load (request, loaderContext);

//now you can draw.
var bitmapData:BitampData = new BitmapData (200, 200);
bitmapData.draw (loader);

Why we need to this? Doing so would instruct Flash Player not to begin downloading the image file until after attempting to download a policy file. If Flash Player successfully finds policy file with right permission, you are set to do Bitmap drawing.

Believe me, that's all you need as long as you meet two criteria (crossdomain.xml and checkPolicyFile=true). I would try to post a working example.

Posted by Abdul Qabiz at January 28, 2007 05:47 PM

Comments

Abdul,

Thx a lot for this post, i spent a lot of time to fix this problem and i had no idea about the reason till i red it...

david

Posted by: BDavid at May 21, 2007 02:06 AM

this can also be achieved in actionscript 2.0 by using
System.security.loadPolicyFile("")

Posted by: richardt at July 3, 2007 03:32 AM

Abdul,

Please provide a example of cross domain solve. i am really confused with this problem which belongs to my current project. i can not understand. Please help me.

Posted by: Avijit at August 2, 2007 06:46 PM

@richardt: No, this doesn't work in AS2.0.

@Avijit: What problem are you facing? There are some cross-domain problems that can be solved using crossdomain.xml or a server-side proxy... In this post we talked about a problem where Flash Player API doesn't let you grab bitmap-data of video/image coming from other domain, even when there is crossdomain.xml (AS3 allows you to load the policy before code-invocation but AS2 doesn't).

However, there is a workaround to grab the bitmap-data (screenshot) of a video from external (YouTube or others) sites without using any crossdomain.xml or server-side script...


Check out the last update (section) in this post:-

http://www.abdulqabiz.com/blog/archives/flash_and_actionscript/bitmapdatadraw.php


-abdul

Posted by: Abdul Qabiz at August 3, 2007 02:16 PM

What if
loaderContext.checkPolicyFile = true;
doesnt work ?

Posted by: Elad Ziv at August 19, 2007 09:10 PM

@Elad: I think, it works in Adobe Flash Player 9, but not in Flash Player 8 or lower.

-abdul

Posted by: Abdul Qabiz at August 20, 2007 11:10 PM

System.security.loadPolicyFile() DOES work in Actionscript 2.0

Tested it myself...

Posted by: Michael Nielsen at September 27, 2007 01:10 AM

Just wanted to confirm, for anybody else, that it DOES WORK in AS 2.0. You just have to force it to load the policy file:



System.security.loadPolicyFile("http://" + myDomain + "/crossdomain.xml");


I put that as soon as I know the domain I'm loading from, and everything works.

Posted by: Augustus at November 22, 2007 07:37 AM

Just thought I'd add my bit, yep it does work in AS2 using the loadPolicyFile method.

It's helped loads! thanks guys,

Posted by: Designer Boy at February 18, 2008 05:37 PM

Hey, so when you write that this code is for flash player 9 and not 8, what you actually mean is that it's for AS3 and not AS2? Cuz flash 9 playes AS2 just the same!

Posted by: Pollo at February 29, 2008 11:13 PM

Hi, thanks for this post, it's very helpful. My problem is that I cannot upload a crossdomain.xml file as it is not my domain ( Amazon S3 ). Is there another way around this problem? I don't really understand why the data is protected in the first place.

Posted by: Eric at March 20, 2008 10:13 AM

@Eric, even if it's S3, you can have crossdomain.xml there.

Anyway, you can use proxy, which is hosted on your server, to load image.

You can check out this:-
http://www.abdulqabiz.com/blog/archives/general/php_proxy_script_for.php

This might be useful, you need to host it on your server and use it like this:-

http://yourserver/proxy.php?url=http://amazons3/image.jpeg

Posted by: Abdul Qabiz at March 20, 2008 10:40 AM

Thank You Abdul :-)

Posted by: Anders at April 30, 2008 10:30 PM

Abdul, you just saved my life :)

Posted by: Justin Love at May 9, 2008 07:00 AM

Post a comment




Remember Me?


Please enter the security code you see here

(you may use HTML tags for style)