Android WebView local html cannot get file from a URL - android

I've a webview that loads a local html file. The problem is simple. It cannot load something from any url, an image, a js file etc.
<html>
<head>
<script src="http://code.jquery.com/jquery-1.12.1.min.js" type="text/javascript"></script>
</head>
<body>
<img src="http://website.com/myimage.jpg" />
</body>
I added necessary permission tag into my manifest file.
<uses-permission android:name="android.permission.INTERNET" />
I'm not using cordova/phonegap. It just a webview that loads a local html file.
WebView webView=(WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
I've been searching since the morning and nothing works. I promise I'm going to pray until end of my life for the one who solves this problem

Try this
Put your file in assets folder and use
webView.loadurl("file:///android_asset/yourfilename.html");
Hope it helps thanks.

Related

Android WebView with css and javascript

Many of the answers I've been seeing here are 6+ years old so I was wondering if I could get a more recent answer as they haven't been working for me. I have an html file file.html which contains a
<LINK href="stylesheet.css" type="text/css" rel="stylesheet">
for the CSS and the following for the javascript:
<script src="myscript.js"></script>
file.html, stylesheet.css and myscript.js are all stored in app/assets/Content/
So I am trying to run it with:
WebView webView = (WebView) findViewById(R.id.event_webview);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("file:///assets/Content/file.html");
However, the WebView is showing the error:
Webpage not available: The webpage at
file:///assets/Content/file.html could not be loaded because
net::ERR_FILE_NOT_FOUND
I have also considered using loadDataWithBaseURL but this requires the data field to contain all of the html which is too big of a file to hardcode.
Does anybody know how I could use html+css+javascript for a WebView?
Your URI for asset folder is incorrect. Change asset to android_asset. As follows:
webView.loadUrl("file:///android_asset/file.html");

Android webview loading css from assets, but not js

Using Android Studio and avd emulator. css defintely loading, js will execute from within head or attached to body tag, but not from external file in assets.
My MainActivity:
WebView webview = new WebView(this);
webview.setWebChromeClient(new WebChromeClient());
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webview.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "UTF-8", null);
setContentView(webview);
My html head:
<link rel="stylesheet" href="my.css" />
<script src="jquery.mobile.min.js" type="text/javascript" />
<script src="my.js" type="text/javascript" />
Test JS (no jquery) that executes in head but not in external my.js:
function var1() {
document.getElementById('jstest').innerHTML = 'hi';
}
window.onload = var1;
My Current sentiment:
Charlie Brown.
Changing to jquery.min.js and getting rid of the mobile version has resolved everything. It probably sent the entire js into tilt, but the inline was processing first.

How to add local css file to a page in WebView?

So, I have a WebView showing page from url:
WebView webView = (WebView) findViewById(R.id.showContentWebView);
String url = "http://edition.cnn.com/2014/02/05/sport/shaun-white-sochi-slopestyle/index.html";
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(url);
And I also have a file "style.css" in my assets folder. How can I display the page in WebView with style.css connected?
I think this is possible only if you somehow process the HMTL content, adding a link to your css file. In this case, please refer to this question.

Android WebView loadWithBaseURL

I am wondering how do I set the path for WebViews loadWithBaseURL correctly.
What I want to do is, to load html in a webview, that uses resources that are stored on the external storage.
For Example:
<html>
<head>
<style>body{ background-image:url(beach.jpg); }</style>
</head>
<body>
<img src="football.jpg" />
</body>
</html>
Where beach.jpg and ball.jpg are stored directly in the "root" directory of the phones external storage (/sdcard/beach.jpg and /sdcard/ball.jpg)
So I tried to load the content as follows:
String html = "<html> ... example from above ... </html>";
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
webView.loadDataWithBaseURL("file://" + base, html, "text/html", "utf-8", null);
However the path seems to be wrong, because I can't see the image in the webview.
Any suggestions?
Have you enabled file access on the webview?
webView.getSettings().setAllowFileAccess(true);
Additionally, if you are constructing the HTML yourself - you might consider using full paths for the images.
String html = "<html>... <img src=\"file://"+base+"/football.jpg\" />";
Have you give internet permission?
<uses-permission android:name="android.permission.INTERNET" />
These may also help..
Android webview loadDataWithBaseURL how load images from assets?
http://myexperiencewithandroid.blogspot.com/2011/09/android-loaddatawithbaseurl.html
Android v2.2-2.3.5: WebView : loadDataWithBaseURL : will only load page once

problem to load swf file in android

hi friends,
i want to load swf file in android emulator.but there is problem to display.
problem is when i run the project display blank white screen. here is the code ::
String url ="file:///android_asset/co.swf";
WebView wv=(WebView) findViewById(R.id.webview);
wv.getSettings().setPluginsEnabled(true);
wv.loadUrl(url);
is there any permission are give to manifest file?
I think you won't be able to access the swf direcly from webview. So instead you have to embed the swf file in html and then call that file instead.
This both files should be placed in assets folder inside your project.
So your HTML file will look somewhat like this
File:co.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8" />
</head>
<body style="margin: 0; padding: 0">
<object id="i1">
<param name = "movie" value = "co.swf">
<embed src = "file:///android_asset/co.swf" ></embed>
</object>
<script>
var x = document.getElementById("i1")
x.setAttribute("height", screen.height);
x.setAttribute("width", screen.width);
</script>
</body>
</html>
In Java file:
setContentView(R.layout.activity_main);
WebView wv = (WebView)findViewById(R.id.webView1);
WebSettings ws = wv.getSettings();
ws.setPluginState(PluginState.ON);
ws.setJavaScriptEnabled(true);
ws.setAllowFileAccess(true);
wv.loadUrl("file:///android_asset/co.html");
And the main xml (activity) should contain a webview possibly with id webView1 which is automatically generated. This should work.

Categories

Resources