0

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>
Dave Bilodeau
  • 249
  • 1
  • 5
  • 13

4 Answers4

1

You can try this:

webView.loadURL( new File(new File("app:/assets/FlexMobile.pdf").nativePath).url);
Maehler
  • 6,111
  • 1
  • 41
  • 46
Rio85
  • 83
  • 7
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

animuson
  • 53,861
  • 28
  • 137
  • 147
0

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

Andrea Gherardi
  • 826
  • 1
  • 9
  • 18
0

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.

Christo Smal
  • 615
  • 5
  • 16
  • Thanks for the response. I am on Android and you were right it doesnt work! Can you suggest an application that will make this work? Is there a better method to get this done? – Dave Bilodeau Jan 24 '12 at 15:17
  • Unfortunately that is all I can tell you. Currently my focus is on iOS. I've tried it on android, didn't work, bad experiment. However I've read that apparently on Android file types get associated with applications, as far as I know you can configure these file types against a default application. I'd suggest start off by typing the url of a local pdf in the browser and see if it opens, if not play with the file associations, and then retry the browser. Should you get a solution, please share. Currently we're working on wrapping the PDFTron lib in an ANE. That is the ultimate solution. – Christo Smal Jan 24 '12 at 17:24
  • Thanks for the info. I will play with it. I really want all these pdfs built into the app so that users dont need internet. But I have been searching like crazy to figure out a solution to this and I cant find it so I may have to just deal until someone smarter than me figures it out! – Dave Bilodeau Jan 24 '12 at 17:41