2

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?

ketan
  • 19,129
  • 42
  • 60
  • 98
marcocb
  • 241
  • 3
  • 10
  • 1
    Can you please try reading the file into a filestream object, and converting that filestream object into bitmapdata for direct display? Then we can see if the error is related to Security, file path issues, or data load corruption. – cbroughton Nov 14 '11 at 09:44

2 Answers2

5

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

marcocb
  • 241
  • 3
  • 10
0

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();
}
user427969
  • 3,836
  • 6
  • 50
  • 75
Attiq Rao
  • 51
  • 3