3

我有下载一个简单文本文件并希望在 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");
    }

]]>

4

1 回答 1

1

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

于 2013-03-01T18:10:20.207 回答