StageWebView for Local HTML file
Copy link to clipboard
Copied
I simply cannot get this to work. I see a lot of people offering advice on the web for how to implement it, I've been trying all day and it doesn't work with Adobe AIR 2.5 for Android Flash CS5 plug-in.
Instead of linking to an external website:
webView.loadURL(http://www.google.com);
I want to use StageWebView (because of the nice scroll capability) to load a local html file. I have tried EVERYTHING, and it doesn't work.
I have included the file in the APK (tried as a subdirectory and eventually, to simplify testing, I just included it in the same directory so no complex path).
I have tried what others had recommended:
webView.loadURL("test.html");
I have tried using the filesystem and resolving the path:
import flash.filesystem.File;
var file:File = File.applicationDirectory.resolvePath("test.html") ;
webView.loadURL(file.nativePath) ;
I have tried numerous change-ups on the filesystem approach, including:
var file:File = new File("app:/test.html");
and
webView.loadURL(file.url);
NONE OF THESE THINGS WORK. Can anyone help me? I'd like to have scrollable content, included inside the Android app - like a license html file or others.
If none of this will work, any suggestions on creating simple, scrollable content that looks nice (scroll pane, scroll list, anything?)
Copy link to clipboard
Copied
The problem is that the StageWebView object cannot load a file with an app: URL.
The two workarounds that I know of are:
- Copy the file to a temporary file and load the temporary file with loadURL().
- Load the file into string and use the loadString() method.
Note that the two methods have different rules as to whether remote or other local resources can be loaded by the HTML.
(See http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/StageWebView.html#loadURL%28%29)
Copy link to clipboard
Copied
Hi Joe,
can you show code snippet for the 1st solution? With loadString you can't load anything else into that page (i..e stylesheet)
Edit: soon after I've posted this question I have found a solution:)
var resultsPageUrl:String = File.applicationDirectory.resolvePath("output-html/result_html_pattern.html").nativePath;
//and later
webView.loadURL(resultsPageUrl);
Edit2: above was just enough to have it working in adl still no joy
Here is an example of temporary file being loaded, this works, http://forums.adobe.com/message/3443946
var source:File = File.applicationDirectory.resolvePath("output-html");//copy entire folder (with css)
var destination:File = File.applicationStorageDirectory;
source.copyTo(destination, true);//copy to the application storage//get path to the html page within copied folder
var resultsPageUrl:String = "file://" + destination.resolvePath("result_html_pattern.html").nativePath;
best regards
Copy link to clipboard
Copied
Thanks to your code snippet. I was struggling for about 3 days on this issue. Thanks again.

Copy link to clipboard
Copied
i have the same problem please tell to me what is the final soluation

Copy link to clipboard
Copied
var initialURL :String;
// This copies a single file into a subdir of appStorageDir
var source:File = File.applicationDirectory.resolvePath("asset/page.html");
// create file(s) in a subdir of appStorageDir to simplify cleanup
var destination:File = File.applicationStorageDirectory.resolvePath("asset/page.html"); // now do the copy and create a ref to our HTML file that a browser will understand source.copyTo(destination, true);
initialURL = "file://" + destination.nativePath ;
webView.loadURL(initialURL);
Workssssssssssssss
Copy link to clipboard
Copied
This method seems to no longer works with the current AIR SDK. Any chance you have a workaround that does?
Copy link to clipboard
Copied
Indeed it does not work any more on air 3.9.
My workaround is to minify the html file and save the content in a AS class. Then use "loadString" instead of "loadUrl".
Copy link to clipboard
Copied
can you share your code routine for this? Im struggling with the same issue and like how you describe your approach to it, just not 100% sure how to write the code from scratch. thanks!
Copy link to clipboard
Copied
The code looks like this:
var testFile : File = File.applicationDirectory.resolvePath("test.html");
var testFileCopy : File = File.createTempFile();
testFile.copyTo(testFileCopy, true);
webViewInstance.loadURL(testFileCopy.url);
Copy link to clipboard
Copied
is the above code still the best option for android?
Copy link to clipboard
Copied
I was able to get this work using this work around from Mike Chambers:
webView = new StageWebView();
webView.stage = this.stage;
webView.viewPort = new Rectangle( 0, 0, stage.stageWidth, stage.stageHeight );
var fPath:String = new File(new File("app:/html/index.htm").nativePath).url;
webView.loadURL( fPath );
A bit of a hack/work around, but it works.
-Thomas
Copy link to clipboard
Copied
Hi,
I have made an extended StageWebView Class that lets you:
It lets you:
* Communicate Actionscript with Javascript.
* Communicate Javascript with Actionscript.
* Load local files and resources in a easy way.
* Extend loadString method with AS3 - JS communication.
By example you can call javascript from as3
// call javascript with callack function
webView.bridge.call('someFunctionToCall', callBackFunction, ...arguments );
// reference local resources in a easy way
<img src="appfile:/image.png">
You can find it at:
http://code.google.com/p/stagewebviewbridge/
I will made some simple tutorials at:
Comments are welcome.
Copy link to clipboard
Copied
I have many times and use all above, none of them works on my real NexusS
use this
var tempsource:File = File.applicationDirectory.resolvePath("html/map2.html");
var tempFiles:File = File.applicationStorageDirectory;
tempsource.copyTo(tempFiles,true);
resultsPageUrl ="file://" + tempsource.nativePath;
or this
public var resultsPageUrl:String = File.applicationDirectory.resolvePath("html/map2.html").nativePath;
it works on FB Simulator and even work on Real Ipad , but none works on Nexus S
yep it is so hard to creat LBS app using AIR.
Copy link to clipboard
Copied
The following works for me on Android (haven't tried specifically on the Nexus S). In general you should use the url property instead of the nativePath property, especially when the target parameter IS a URL.
var webView:StageWebView = new StageWebView();
webView.stage = this.stage;
webView.viewPort = new Rectangle( 0, 0, this.stage.stageWidth, 60 );
//Copy the html file outside the app directory
var templateFile:File = File.applicationDirectory.resolvePath( "adview.html" );
var workingFile:File = File.createTempFile();
templateFile.copyTo( workingFile, true );
try
{
webView.loadURL( workingFile.url );
}
catch (e:Error)
{
trace( e );
}

Copy link to clipboard
Copied
You can try to use this:
var f:File = File.documentsDirectory.resolvePath(String(data));
this.swv.loadURL(f.url);
data is the direction to file html
swv is the StageWebView object item.
Copy link to clipboard
Copied
I have opened a feature request here:
Please vote for it!
