How to display local PDF in Flex Mobile Project?

蹲街弑〆低调 提交于 2019-12-25 02:27:05

问题


I am running Flex mobile project and I would like my mobile app to be able to load and display pdf files that are saved locally. I do not want to go out to the web to access these pdf files.

I cant figure out how to make this code work for a locally saved file.

Error MSG: "Error #2044: Unhandled ErrorEvent:. text=Load error."

Does anyone have a suggestion?
Thanks!

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark"
    overlayControls="true" title="PDF Display"
    viewActivate="view1_viewActivateHandler(event)"
    viewDeactivate="removeStageWebViewHandler(event)">

<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;
        import spark.events.ViewNavigatorEvent;

        private var myWebView:StageWebView;

        protected function view1_viewActivateHandler(event:ViewNavigatorEvent):void {
            myWebView = new StageWebView();
            myWebView.viewPort = new Rectangle(5,60,stage.stageWidth-10,stage.stageHeight-140);
            myWebView.stage = this.stage;
            myWebView.loadURL( "assets/sample.pdf");
        }

        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
            if (myWebView) {
                var point:Point = localToGlobal(new Point());
                myWebView.viewPort = new Rectangle(5,60,stage.stageWidth-10,stage.stageHeight-140);
            }
        }  

        protected function goBackHandler(event:MouseEvent):void {   
            navigator.popToFirstView();
        }

        protected function removeStageWebViewHandler(event:ViewNavigatorEvent):void {
            myWebView.stage = null;
            // just remove the target and will leave.
        }

    ]]>
</fx:Script>

<s:actionContent>
    <s:Button label="BACK" click="goBackHandler(event)"/>
</s:actionContent>
<s:Image x="-1" y="2" source="assets/eventsback.jpg"/>

</s:View>

回答1:


The Android Issue is actually a fundamental Android Browsers OS Issue. Think of the AIR StageWebView as a Hole carved out of your app showing you the browser and all its capabilities.

If your OS supports viewing PDFs, then your App's StageWebView will also. on Android 1.0 - 3.0 when you try and view a PDF it will download the doc it and Hand Shake it off onto another application like Acrobat.

leaving to do something like: http://forums.adobe.com/thread/864113

Hopfully this will be fixed in the newest version of Android...4.0




回答2:


You can try this:

webView.loadURL( new File(new File("app:/assets/FlexMobile.pdf").nativePath).url);



回答3:


Your code should work for iOS. However you have a bug in the following line:

myWebView.loadURL( "assets/sample.pdf");

The url you specify is not valid. It must be a fully qualified URL. Try to use the following or something similar instead:

myWebView.loadURL( File.documentsDirectory.resolvePath("assets/sample.pdf").url );

If you are on android, the above might not work straight out of the box. You might need to install an application that plugs into the browser that is capable of parsing PDF's.




回答4:


I've tried the solution proposed by Christo on an iPad, and it comes out with an error:

"Protocol not supported"

that's because using File.resolvePath.url gives a URL like this:

app://blahblah/blabla.

I've solved this problem using nativePath, so

myWebView.loadURL( File.documentsDirectory.resolvePath("assets/sample.pdf").nativePath );

hope this helps



来源:https://stackoverflow.com/questions/8979303/how-to-display-local-pdf-in-flex-mobile-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!