Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.

Posts: 39
Registered: ‎01-13-2011
My Device: Not Specified
Accepted Solution

QNXStageWebView broken for local files now?

[ Edited ]

Hello all,


I'm trying to load a string as html in a QNXStageWebView and am having difficulty doing so. The code looks like this:


entryWebView = new QNXStageWebView();

entryWebView.enableCookies = true;

entryWebView.addEventListener(Event.COMPLETE, loadEntryWebView);



This doesn't work; I see a blank, white webView window. By the way, entryWebView is a class variable declared as follows: 


private var entryWebView:QNXStageWebView = null;


Ok, I figure loadString might not be implemented. So I try the following, which I read on these very boards as being a solution to needing to load local files. Now, because this is dynamic content, I would have to save an html file, then load it. Obviously not a long-term solution, but fine enough until loadString works.


entryWebView = new QNXStageWebView();

entryWebView.enableCookies = true;

entryWebView.addEventListener(Event.COMPLETE, loadEntryWebView);

entryWebView.loadURL( File.applicationDirectory.nativePath + "file.html");


where the file is of course named file.html and stored under the src folder in my project.


This displays an error message in the web view, which reads: Error. The browser does not know how to handle this web page. Try again.


I figure the html in my file might be malformed... Yet no matter what html I try, this same error pops up.

Just to make sure I'm doing everything else right, I check if the entryWebView will load google when given the url. It does.


What am I doing wrong? 


Thanks in advance.


EDIT: Tried one more thing. I specified an invalid path and the same error message as above is received.

Posts: 153
Registered: ‎10-26-2010
My Device: Bold 9900
My Carrier: AT&T

Re: QNXStageWebView broken for local files now?



    I had this problem.  You just need to prepend "file://" to make your filespec into a URL and it will work.



Accepted PlayBook Applications: Marmi-doos, BASIC, Run and Hide, SuperClip
Accepted OS4-7 SmartPhone App: KeepLightOn
Posts: 165
Registered: ‎10-26-2010
My Device: BlackBerry® PlayBook™
My Carrier: .

Re: QNXStageWebView broken for local files now?

[ Edited ]

Try saving to and loading from File.applicationStorageDirectory instead of File.applicationDirectory


Next, use this to load your page:



entryWebView.loadURL( File.applicationStorageDirectory.url + "/file.html");


AIR leaves off ending slashes when forming paths and URLs, so you have to add them before any filenames or extra directories you'd like to use. The way you're trying to load it would look like this:





Posts: 39
Registered: ‎01-13-2011
My Device: Not Specified

Re: QNXStageWebView broken for local files now?

[ Edited ]

Thanks for your responses. I have solved my issue. If you're interested in the solution, skip to the bottom of this post. If you're interested in how I figured this out, read on. Apologies in advance for the atrocious formatting of this post; suffice to say these forum's text editor doesn't agree with me.

Here is what I have found out since my first post:

Testing if a url is valid by using flash.filesystem.File instances and the class property exists, I have found that "app:/file.html" or ("file:" + File.applicationDirectory.nativePath + "/file.html") are the only urls that result in File.exists returning true. The following all return false or result in an exception upon attempting to set the File.url value:



"file:" + File.applicationDirectory.nativePath + "/file.html" does exist, as previously stated, resulting in a path much like the following: file:/accounts/1000/appdata/APPLICATION_ID/app/air/file.html

"app:" + File.applicationDirectory.nativePath + "/file.html" does not exist.
"app:/" + File.applicationDirectory.nativePath + "/file.html" does not exist.
"file:/" + File.applicationDirectory.nativePath + "/file.html" does not exist.
File.applicationDirectory.nativePath + "/file.html" does not exist.
File.applicationDirectory.url returns "app:/", so File.applicationDirectory.url + "file.html" exists and is equal to app:/file.html.
File.applicationDirectory.url + "/file.html" does NOT exist and is equal to app://file.html. It returns an exception upon attempting to assign this string to File.url.
I tried to use both file paths which existed by testing them with an IconButton. Of course, I used .png files instead of .html. app:/icon.png worked, file:" + File.applicationDirectory.nativePath + "/icon.png" did not work, resulting in the following error: "Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found."
Finally I tried both paths with the web view.
app:/file.html did not work.
("file:" + File.applicationDirectory.nativePath + "/file.html") did work.
With files in the File.applicationStorageDirectory, entryWebView.loadURL( File.applicationStorageDirectory.url + "/file.html" does not work.
File.applicationStorageDirectory.url results in a path that starts with app-storage:/. According to the QNXStageWebView documentation, only file:/ paths are supported. Thus the nativePath must be used for this situation as well. The following code works: ("file:" + File.applicationStorageDirectory.nativePath + "/file.html"). 
Summary: Use ("file:" + File.applicationDirectory.nativePath + "/file.html") for QNXStageWebView, if loading a static bundled html file. Use ("file:" + File.applicationStorageDirectory.nativePath + "/file.html"), if loading a dynamic file in the applicationStorageDirectory. Use (File.applicationDirectory.url + "icon.png" ) for IconButton.
Here is some code which saves a string (presumably html) to a file and then loads that file in a QNXStageWebView. 



		        var htmlFile:File = File.applicationStorageDirectory;
			htmlFile = htmlFile.resolvePath( "file.html" ); 

			var htmlToWrite:String = "<b>This is some HTML</b>";
	// write the file
			var fs:FileStream = new FileStream();
			fs.open( htmlFile, FileMode.WRITE );
			fs.writeUTFBytes( htmlToWrite );
	// open saved html file in webview
		var htmlFilePath:String = ( "file:" + htmlFile.nativePath );
                var webView:QNXStageWebView = new QNXStageWebView();
                var rect:Rectangle = new Rectangle( 0, 0, 1024, 600 );
	        webView.viewPort = rect;
		webView.stage = stage;
                webView.loadURL( htmlFilePath );
Posts: 439
Registered: ‎10-25-2010
My Device: Not Specified

Re: QNXStageWebView broken for local files now?

[ Edited ]


File.applicationDirectory.nativePath + "/file.html" does not exist.




var file:File = File.applicationDirectory.resolvePath("file.html");





Posts: 39
Registered: ‎01-13-2011
My Device: Not Specified

Re: QNXStageWebView broken for local files now?

file.nativePath, in your code, is equal to 




The call to webView seems to need to start with file:/. So webView.loadURL("file:" + file.nativePath) works.

Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: QNXStageWebView broken for local files now?

[ Edited ]

I currently have the same problem when testing with loadString(), so I decided to track down the source of the problem with loadURL() and local files.


As you've discovered, QNXStageWebView can't handle app: or app-storage: URLs.  So for local stuff, we have to give it a file: URL.


Unfortunately, when you use File.applicationDirectory as the base (whether by incorrectly just concatenating "/file.html" or by correctly using resolvePath("file.html)), the url prefix that is stuck in there remains app:.


What you're doing by grabbing the nativePath is a start, but you're going about it wrong, technically.  That's not to say it won't work in your particular case... just that it might not always work and there are a few alternatives that are safer.


The reason it's incorrect is that URLs are encoded, while native paths are not.  When you use one as the other, you would likely run into troubles whenever you have special characters involved, like spaces.


Here's a safe alternative.  Use File.applicationDirectory.resolvePath('file.html') to get an app: reference to the file, then take the native path and use that to build a new File object:


var appPath:File = File.applicationDirectory.resolvePath('file.html');
var filePath:File = new File(appPath.nativePath);
// Note: use  of "new File()" and filePath.url


Another option is to find the path relative to the current directory of the application when it runs.  To do this, you need to do this:


// in your constructor
NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, onInvoke); // use current dir from event, and PB-specific relative path private function onInvoke(e:InvokeEvent):void { var path:File = e.currentDirectory.resolvePath('app/air/file.html'); webView.loadUrl(path.url); }


 I don't like that, as it depends on currently undocumented (except in Elena's sandbox layout post) information about where the folder would be.



As it turns out, however, there's another approach you might like: ;-)


var content:String = "<html><body><p>test page</p></body></html>";
webView.loadStringWithBase(content, '');


Yep, you guessed it.  Although loadString() fails for unknown reasons, using the above and any base (any that I've tried, including empty string) seems to work.  Kudos to me! Smiley Happy


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Posts: 39
Registered: ‎01-13-2011
My Device: Not Specified

Re: QNXStageWebView broken for local files now?

[ Edited ]

Amazing, I never would have thought loadStringWithBase would work when loadString doesn't. Thanks for your informative post. I had already seen that nativePaths weren't encoded and had been working around that elsewhere in my code; your method makes this much simpler. It is interesting that your method produces a url which begins with file:///accounts/... Apparently, both file:/ and file:/// are accepted.


Thanks again.



Posts: 33
Registered: ‎01-15-2011
My Device: Not Specified

Re: QNXStageWebView broken for local files now?



I realize this is an old thread, but I have been having a very similar issue and I've been pulling my hair out all day trying to figure it out. I am trying to navigateToURL() with a local file. I feel as if I have exhausted all possibilities. 


Here is the thread: similar thread


Any help would be GREATLY appreciated!!


Thank you!

Posts: 23
Registered: ‎12-17-2010
My Device: Not Specified
My Carrier: Software Engineer

Re: QNXStageWebView broken for local files now?

[ Edited ]

THANKS......I was in the midst of the same problem for a very long time.....Its BRILLIANT...