android - 如何在 AIR 中访问存储在 File.applicationStorageDirectory 中的文件,适用于 Android

标签 android apache-flex actionscript air

我有下载简单文本文件并希望在 StageWebView 中显示的 ActionScript 代码。文件下载并成功保存在 File.applicationStorageDirectory 中。我可以将内容输出到控制台,但是,我无法在 StageWebView 中显示该文件。根据我读过的所有内容,这应该可行。 为什么我无法在 Android 上显示我在本地下载并保存的文件?

请注意,我无法更改 StageWebView 以从远程位置打开文件,因为用户需要能够在离线模式下访问该文件。

我也无法将文件存储在 sdcard 上,因为我不想让用户访问该文件。

这是一个示例测试,演示了我正在尝试做的事情。我正在使用 Apache Flex 4.9.0 并部署到 Android 4.1.2

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" applicationDPI="160" >
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

    protected var webView:StageWebView = new StageWebView();
    public static const LOCAL_FILE_STORE:File = File.applicationStorageDirectory;

    protected function onClick(e:Event):void{

        trace("File.applicationStorageDirectory: "+ File.applicationStorageDirectory.nativePath);
        trace("File.desktopDirectory: "+ File.desktopDirectory.nativePath);
        trace("File.applicationDirectory: "+ File.applicationDirectory.nativePath);
        trace("File.documentsDirectory: "+ File.documentsDirectory.nativePath);


        var loader: URLLoader = new URLLoader();
        var request:URLRequest = new URLRequest("http://www.apache.org/dist/flex/4.9.1/README");
        loader.dataFormat= URLLoaderDataFormat.BINARY;
        loader.addEventListener(Event.COMPLETE, completeHandler);
        loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
        loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
        try {
            loader.load(request);
        } catch (error:Error) {
            trace("Unable to load requested document.");
        }
    }

    private function completeHandler(event:Event):void {
        var urlLoader:URLLoader = URLLoader(event.target);

        var writeFile:File = File.applicationStorageDirectory.resolvePath("test.txt");

        var writeStream:FileStream = new FileStream();
        writeStream.open(writeFile, FileMode.WRITE);
        writeStream.writeBytes(urlLoader.data);
        writeStream.close();

        //Test if we can output contents to console
        var readStream:FileStream= new FileStream();
        readStream.open(writeFile, FileMode.READ);
        var content:String= readStream.readUTF();
        trace("File Contents: "+ content);

        //Test if we can display file in stagewebview
        var sView:StageWebView= new StageWebView();
        sView.stage= this.stage;
        sView.viewPort= new Rectangle(0, 40, stage.stageWidth, stage.stageHeight);
        sView.addEventListener(ErrorEvent.ERROR, handleError);
        sView.addEventListener(Event.COMPLETE, handleComplete);

        trace("Loading file://"+writeFile.nativePath);
        sView.loadURL("file://"+writeFile.nativePath);
    }

    protected function onError(event:ErrorEvent):void
    {
        trace("UH OHHHH " + event);
    }

    private function ioErrorHandler(event:IOErrorEvent):void {
        trace("ioErrorHandler: " + event);
    }

    private function securityErrorHandler(event:SecurityErrorEvent):void {
        trace("securityErrorHandler: " + event);
    }

    private function httpStatusHandler(event:HTTPStatusEvent):void {
        trace("httpStatusHandler: " + event);
    }

    protected function handleError(event:ErrorEvent):void
    {
        trace("GOT ME AN ERROR: "+ event.errorID + "  "+ event.text);
    }

    protected function handleComplete(event:Event):void
    {
        trace("Done");
    }

]]>

最佳答案

所以肯定是 Apache Flex 4.9.0 有一个 bug,因为当我更新到 Flex 4.9.1 时,上面的代码开始工作了。

关于android - 如何在 AIR 中访问存储在 File.applicationStorageDirectory 中的文件,适用于 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15160108/

相关文章:

android - 尝试在 Android 上运行 Python - 使用 cmake

javascript - 从 Flex 调用 JavaScript

apache-flex - 仅 Actionscript 项目中的 Flex(mx 包)类?

flash - 如何使用 flash actionscript 2 更改文本颜色?

java - 一般文件结构及/bin和/dist目录的区别

actionscript-3 - 类似 Ruby 的问题 : Make this function shorter (ActionScript 3)

javascript - 如何使用PhoneGap下载并保存文件(APK)到android SD卡中?

java - 使用 openCv 的嘴部检测仅在一张脸上返回很多圆圈

android - progressBarStyle 不适用于 Android Studio

actionscript-3 - TextField() 设置插入符号位置