在 iPad Flex4.5 应用程序中从 File.applicationStorageDirectory 保存/读取文件

发布于 2024-12-15 06:07:08 字数 1063 浏览 3 评论 0原文

我有一个在 iPad 上运行的 Flex 应用程序(SDK 4.5.1)...我需要下载任何文件,将它们放在本地目录(如 File.applicationStorageDirectory)中,然后在我的应用程序中查看该文件。

因此,在我的测试应用程序中,使用 urlLoader 类下载了 png 图像。

这是下载的完整处理程序:

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"
  }
}

在我的 iPad 上,我可以看到下载的文件存在,但是当我运行 immagine0.source = pathFile (这是一个图像组件)行时,什么也没有出现......也许我可以写一个文件,但我无法读取它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

闻呓 2024-12-22 06:07:09

经过 6 个小时的调试和编码...我用一个非常简单的解决方案解决了这个问题...
更改了行

var pathFile:String = file.nativePath;

var pathFile:String = file.url;

他以这种方式解决了 file.url:

app-storage:/img.png

。现在可以了!希望这篇文章对其他人有这个问题有帮助。谢谢大家

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

梦里人 2024-12-22 06:07:09

以下两个函数是在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();
}

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文