Saving/Reading a file from File.applicationStorageDirectory in an iPad Flex4.5 app

marcocb picture marcocb · Nov 14, 2011 · Viewed 9.3k times · Source

I have a Flex application (SDK 4.5.1) which runs on iPad... I need to download any files, put them in local directory (like the File.applicationStorageDirectory) and then view the file inside my application.

So in my test application a downloaded a png image using the urlLoader class.

Here it is the complete handler of the download:

private function onComplete3(event:Event):void{
 try{
    var ba:ByteArray  = event.target.data as ByteArray;
    var file:File=File.applicationStorageDirectory.resolvePath("img.png");
    var pathFile:String = file.nativePath;
    var fileStream:FileStream = new FileStream();  
    fileStream.open(file, FileMode.WRITE);  
    fileStream.writeBytes(ba);  
    fileStream.addEventListener(Event.CLOSE, fileClosed);  
    fileStream.addEventListener(IOErrorEvent.IO_ERROR,function(e:IOErrorEvent):void{
       status0.text = "STATE : ERROR 3"
    });
    fileStream.close();     
    status0.text = "STATO : OK";
    path0.text = pathFile; 
    immagine0.source = pathFile; 
  catch(e:Error){
    status0.text = "STATE : ERROR 2"
  }
}

On my iPad I can see the downloaded file exists, but when I run the line immagine0.source = pathFile (which is an image component), nothing appears... Maybe I can write a file but I cannot read it?

Answer

marcocb picture marcocb · Nov 14, 2011

After 6 hours of debug and coding...i solved this problem with a very simple solution... changed the line

var pathFile:String = file.nativePath;

with

var pathFile:String = file.url;

He solved the file.url in this way:

app-storage:/img.png

.Now it works! Hope this post will be helpful for someone other have this problem..Thanks to all