Loading Images in ActionScript 3

I took forever to figure out why I was encountering this error when trying to load images and compiling with mxmlc. Consulting the log file (~/Library/Preferences/Macromedia/Flash Player/Logs/flashlog.txt), I would have this error when trying to view the SWF with the standalone Flash Player:

SecurityError: Error #2148: SWF file
file:///Users/stephen/flex/data/interface/LoadImage.swf cannot access local resource file:///Users/stephen/flex/data/interface/assets.jpg.
Only local-with-filesystem and trusted local SWF files may access local resources.
	at flash.display::Loader/get content()
	at LoadImage/initListener()

It turns out the solution is simple if you know where to look. The configuration file needed to be changed for my Flex 3 SDK compiler. The configuration file is loaded from here in my install:

~/flex/frameworks/flex-config.xml

I needed to change the use-network setting to a value of false instead of true:

   <!-- Enables SWFs to access the network. -->
   <use-network>false</use-network>

Then I could load my image with no problem.

package {
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;

	public class LoadImage extends Sprite {
		private var loader:Loader;

		public function LoadImage() {
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.INIT, initListener);
			loader.load(new URLRequest("asset.jpg"));
		}

		private function initListener (e:Event):void {
			addChild(loader.content);
		}
	}
}
Advertisements

About this entry