Select and access local SWF files within an AIR application

Well, it took me a little time to figure out how to select and load a local SWF file into an SWFLoader instance. Several times I got this error message:

SecurityError: Error #3015: Loader.loadBytes() is not permitted to load content with executable code.

After some research I found this “dreaming in flash” blog entry which basically contains the solution I was looking for.

But then it took me a little more time to figure out how to use this with the SWFLoader. So here is a basic code example:

import flash.display.Loader;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.system.LoaderContext;
import flash.utils.ByteArray;
import mx.controls.SWFLoader;
 
// File reference
var swfFile: File;
 
// Open the SWF file
var fileStream: FileStream = new FileStream();
    fileStream.open( swfFile, FileMode.READ );
 
// Read SWF bytes into byte array and close file
var swfBytes: ByteArray = new ByteArray();
    fileStream.readBytes( swfBytes );
    fileStream.close();
 
// Prepare the loader context to avoid security error
var loaderContext: LoaderContext = new LoaderContext();
    loaderContext.allowLoadBytesCodeExecution = true; // that's it!
 
// Now you could use this with a Loader instance
var loader: Loader = new Loader();
    loader.loadBytes( swfBytes, loaderContext );
 
// Or you could use this with a SWFLoader instance
var swfLoader: SWFLoader = new SWFLoader();
    swfLoader.loaderContext = loaderContext;
    swfLoader.source = swfBytes;

This entry was posted in AIR, Flash, Flex. Bookmark the permalink.

5 Responses to Select and access local SWF files within an AIR application

  1. pedro says:

    Nice example, we’re always glad to help :)

  2. Chen.li says:

    Thank you ,i am just looking for this. i want to load images ,png,jpeg by ByteArray for SWFLoader.

    God bless u.

  3. Bart says:

    Does this still work in the latest Air version?

    when I use this the loader.content stays null

  4. derRaab says:

    Hm. I don’t know. Maybe not anymore? Tell us…

  5. Kojo says:

    Thanks, dude. This helped a lot.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">