1
votes

I'm having an issue with loading an image using the Loader class. Can someone see what I'm doing wrong?

// get file folder location
var file = new File(File.applicationStorageDirectory.nativePath);

// convert to string
var fileString:String = file.url.toString();

// remove string characters
    fileString = fileString.split('file:///').join('');

// create loader
var loader:Loader = new Loader();

// create request
 var urlReq:URLRequest = new URLRequest(fileString+'/logo.jpg');

// load request
   loader.load(urlReq);

When I test it gives me a 'Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found.' If I use a loader.contentLoaderInfo to check IOERROR.IO_ERROR it gives me '1119 Access of possibly undefined propety IOERROR through a reference with static type flash.display:Loader'

Any thoughts to what I'm doing wrong? I've also tried just loading the .jpg from the same folder

var urlReq = new URLRequest('logo.jpg');

that the test app is in but still get the "URL Not Found"

Any help would be greatly appreciated. Thank you.

1

1 Answers

0
votes

This is simple: Don't use Loader, use FileStream instead. Since the file is saved into the app storage dir (or I assume it is, at least), you can read it directly rather than using a Loader.

var file:File = File.applicationStorageDirectory.resolvePath( "logo.jpg" );
var fs:FileStream = new FileStream();
fs.open( file, FileMode.READ );
var bmp:Bitmap = fs.readObject();
fs.close();

this.addChild( bmp );

You should avoid using Loader as much as possible. There is a lot of extra weight in the Loader class that adds can be a drain on performance. Use Bitmap instead, which is the lowest level way of display an image, and wrap it in a Sprite (or use Image instead of Bitmap and Sprite if using Flex) if you need to give it interactivity.