Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a file stored in File.applicationStorageDirectory in AIR, for Android

I have actionscript code that downloads a simple text file and wish to display in StageWebView. The file downloads and is saved in File.applicationStorageDirectory successfully. I can output the contents to the console, however, I CANNOT display the file in StageWebView. According to everything I have read, this should work. Why can I not display a file I downloaded and saved locally, on Android?

Please note, I cannot change StageWebView to open the file from the remote location because a user needs to be able to access the file in offline-mode.

I also cannot store the file on the sdcard because I do not want the user to have access to the file.

Here is a sample test, that demonstrates what I am trying to do. I am using Apache Flex 4.9.0 and deploying to 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");
    }

]]>

like image 752
Patrick Avatar asked Dec 14 '25 15:12

Patrick


1 Answers

So there must be a bug in Apache Flex 4.9.0, because when I updated to Flex 4.9.1, the above code started to work.

like image 178
Patrick Avatar answered Dec 17 '25 08:12

Patrick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!