I am having difficulty in loading a html file from my project assets folder into a webview. I have looked at dozens of tutorials and solutions but none seem to work for me.
In my project's assets folder I have two simple html files. index.html and faq.html
(The plan is to utilise this structure for my help documentation)
My code:
WebView wv = (WebView)findViewById(R.id.webview1);
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
wv.loadUrl("file:///android_asset/index.html");
The webview displays the following:
Web Page Not Available
The Web Page at file:///android_asset/index.html could not be loaded as:
The requested file was not found. index.html
From everything I have read what I have here should work, but it does not.
your usage is right, so if has this problem, you need check the index.html file existed or not carefully, also you can clean the project, and rebuild it.
You can try this code ....
WebView myBrowser;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String myURL = "file:///android_asset/index.html";
myBrowser=(WebView)findViewById(R.id.mybrowser);
/*By default Javascript is turned off,
* it can be enabled by this line.
*/
myBrowser.getSettings().setJavaScriptEnabled(true);
myBrowser.setWebViewClient(new WebViewClient());
myBrowser.loadUrl(myURL);
}
Related
I've developed a small group of .html pages that are stored in a server.
My android app, using a webview with: setWebChromeClient loads these pages.
minSdkVersion 19
targetSdkVersion 24
The problem:
Everytime I need to load a new page, using a link in my .html page, the new page is opened in an external browser.
Url Overriding
I know about the shouldOverrideUrlLoading() method, when we're using the setWebViewClient().
But unfortunately I can't use the normal webview. I need to use the WebChromeClient() because of some feature that only work with this one. (Like the input file)
My doubt is...
How can I override my URL to force them to load inside of the webChromeClient?
I tried this but with no luck:
webView.setWebChromeClient(new WebChromeClient() {
// (...)
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
You can use following code to achieve this.
WebView web = (WebView)findViewById(R.id.web);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportMultipleWindows(true); // This forces ChromeClient enabled.
web.setWebChromeClient(new WebChromeClient(){
#Override
public void onReceivedTitle(WebView view, String title) {
getWindow().setTitle(title); //Set Activity tile to page title.
}
});
web.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
I was using an embedded mobile web application in an Android App making use of the WebChromeClient class and did not want to fiddle around with recompiling the APK.
While looking for the easiest header(); solution (since the mobile web application is in php) I found out that using:
header("Location: url.php", TRUE, 307);
was a quick fix solution without recompiling the Android app.
This allowed the user to re-direct within the app without calling the web browser externally from my app.
Here is the link to the answer by Arindam Nayak where I got the idea from.
I have read several topics here on SO about this, at first all of them seem related but than when i read the "solution" code i cannot understand where the String that keeps the code is.
What i want to do is load a local HTML file, than modify it with some javascript. And after i have modified it i would like to either replace the unmodified HTML file with the modified HTML file, or create a new HTML file from the modified HTML. So after this process i would have the modified HTML file saved on the users SD card.
I would have loved it if there where a functions like this:
String htmlContent = myWebView.getSouce();
Than i could just create an HTML file from that String and save it to the sd card.
This is my code so far.
final WebView webview = (WebView)rootView.findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url)
{
/* This loads a javascript to the Html file and changes its design*/
view.loadUrl("javascript:(function() { "+
"Some Modifications to the test.html file"+
"})()");
//Now when i have modified the above test.html i would like
//to get the modified HTML (The HTML now displaying in my webview).
//So i was hoping i could write something like this:
// String htmlContent = view.getSouce();
}
});
webview.loadUrl("file:///android_asset/test.html");
If you don't understand javascript interfaces:
http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String)
http://developer.android.com/guide/webapps/webview.html#BindingJavaScript
I am new to android programming and I am developing my first apps using eclipse.
I have kept my html and jquery codes inside assets folder.
now this is my code in mainactivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView)findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
// webView.setWebChromeClient(new WebChromeClient);
webView.loadUrl("file://android_asset/www/index.html");
}
when i installed app on my device and run it,
it says,
webpage not available,
the file at file://android_asset/www/index.html may have been moved permanently
plz help me to solve this
Try changing
webView.loadUrl("file://android_asset/www/index.html");
by
webView.loadUrl("file:///android_asset/www/index.html"); // please see the extra forward slash, they have to be 3.
So I have been looking everywhere and doesn't seems to find working answer for this. I have dynamic html page create using iMapBuilder and it has 3 external js files. Before I have added dynamic contents html was working just fine being under assets directory with following command:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ground_floor);
WebView wv;
wv = (WebView) findViewById(R.id.webview);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl("file:///android_asset/imap5custom.html");
}
}
After adding dynamic content with 3 Java Script external files which are also under root assets folder, html doesn't work any more. I have tried:
mWebView.loadDataWithBaseURL
but still didn't work for me.
I have the following situation:
AS3 Code:
...
Security.allowDomain("*");
ExternalInterface.addCallback("onZoom", onZoom);
...
public function onZoom(...
JavaScript code:
...
alert(getFlashMovie("test"));
alert(getFlashMovie("test").onZoom);
...
And in OBJECT and EMBED tags I have allowScriptAccess="always"
If I test the html page from my computer ("file:///path-to-file.html") both
alert(getFlashMovie("test"));
alert(getFlashMovie("test").onZoom);
give me proper result.
If I put my html as well as my swf in my android phone sdcard and I do load it into a WebView like this...
webView = (WebView) findViewById(R.id.webViewMain);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginsEnabled(true);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///sdcard/test/test.html");
... then alert(getFlashMovie("test").onZoom); gives me "undefined".
If I move my html and swf to a web server, and load with webView.loadUrl("http:/mydomain.com/test/test.html"); it works.
I did another try with this example:
http://0me.me/demo/adobeflash/ExternalInterface.call/demo.html
It works if I load it from that url, it doesn't if I copy that html file (and the relevant swf) on my sdcard.
If I copy the same file (and swf) on my computer and test it locally, there it works.
So it seems that flash player for computer allows ExternalInterface.addCallback even for local files (file:///...) whereas flash player for android 10.2 doesn't.
I didn't try installing a local web server on my android phone and loading from there so far, but I would avoid such a solution if possible.
Can someone help me please?
Thanks.