marcocb
marcocb

Reputation: 241

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

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?

Upvotes: 2

Views: 9349

Answers (2)

Attiq Rao
Attiq Rao

Reputation: 51

The following 2 functions are to write and read file in Flex 4.5.

protected function button1_clickHandler(event:MouseEvent):void
{
    var file:File = File.applicationStorageDirectory.resolvePath("samples/test.txt");
    var stream:FileStream = new FileStream()
    stream.open(file, FileMode.WRITE);
    stream.writeUTFBytes(contents.text);
    stream.close();
}
protected function button2_clickHandler(event:MouseEvent):void
{
    var file:File = File.applicationStorageDirectory.resolvePath("samples/test.txt");
    var stream:FileStream = new FileStream()
    stream.open(file, FileMode.READ);
    results.text = stream.readUTFBytes(stream.bytesAvailable);
    stream.close();
}

Upvotes: 0

marcocb
marcocb

Reputation: 241

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

Upvotes: 5

Related Questions