flex - How to access a file stored in File.applicationStorageDirectory in AIR, for Android -
i have actionscript code downloads simple text file , wish display in stagewebview. file downloads , saved in file.applicationstoragedirectory successfully. can output contents console, however, cannot display file in stagewebview. according have read, should work. why can not display file downloaded , saved locally, on android?
please note, cannot change stagewebview open file remote location because user needs able access file in offline-mode.
i cannot store file on sdcard because not want user have access file.
here sample test, demonstrates trying do. using apache flex 4.9.0 , deploying 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 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 can output contents console var readstream:filestream= new filestream(); readstream.open(writefile, filemode.read); var content:string= readstream.readutf(); trace("file contents: "+ content); //test if 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 error: "+ event.errorid + " "+ event.text); } protected function handlecomplete(event:event):void { trace("done"); } ]]>
so there must bug in apache flex 4.9.0, because when updated flex 4.9.1, above code started work.
Comments
Post a Comment