Webview Load HTML from Assets Directory

Webview load html from assets directory

You are getting the WebView before setting the Content view so the wv is probably null.

public class ViewWeb extends Activity {  
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView wv;
wv = (WebView) findViewById(R.id.webView1);
wv.loadUrl("file:///android_asset/aboutcertified.html"); // now it will not fail here
}
}

How to load Html file from assets folder into WebView

All thanks to @CommonsWare for providing me with this wonderful solution
I made this answer for others who have this type of problem.
It is working now. This is exactly how i put it to work


protected WebView webView;
private static final String URL = "file:///android_asset/dancerkate.html";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

webView = (WebView)findViewById(R.id.local_tv01);
webView.setWebViewClient(new WebViewClient());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
}



refreshWebView(webView);

}

public void refreshWebView(View view) {
webView.loadUrl(URL);
}

Loading html file to webview on android from assets folder using Android Studio

The directory name should be assets not android_assets

Do like this: Sample Image

As shown in the above pics just right click on your app->New->Folder->Assets Folder

Now put your .html file here in assets folder.

That's it. Done.

Remaining is same in code what you did.

WebView view = new WebView(this);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl("file:///android_asset/hello.html");
setContentView(view);

Webview load html from assets directory and send data to

There are two ways.

First way

In javascript

function getParameters(query) {
var parts = query.split('&');
var params = {};
for (var i = 0, ii = parts.length; i < ii; ++i) {
var param = parts[i].split('=');
var key = param[0];
var value = param.length > 1 ? param[1] : null;
params[decodeURIComponent(key)] = decodeURIComponent(value);
}
return params;
}
document.addEventListener("load", function() {
var params = getParameters(document.location.search.substring(1));
if(params.hasOwnProperty('text_box_value')) {
var textBox = document.getElementById(<ID of your textbox>);
textBox.value = params['text_box_value'];
}
}
);

In Java code

wv.loadUrl("file:///android_asset/aboutcertified.html?text_box_value=sometext");

Second way

In Java code

public class ViewWeb extends Activity {  
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView wv;
wv = (WebView) findViewById(R.id.webView1);
wv.setWebViewClient(new CustomWebViewClient());
wv.loadUrl("file:///android_asset/aboutcertified.html");
}
}

private class CustomWebViewClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl("javascript:(function setText() {var textBox = document.getElementById('your textbox id'); textBox.value = '" + yourTextString + "';})()");
}
}

Loading index.html from assets into WebView but other files referenced in index.html can't be found

I had the same issue once. I have added the complete paths and it worked. Please add the path,

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
...
<link rel="stylesheet" href="file:///android_asset/www/styles.css">
...
</head>

<body>
<app-root></app-root>
...
<script type="text/javascript" src="file:///android_asset/www/runtime.js">
</script>
...
</body>

</html>

and respectively to all other files.

Webview load all HTML files from assets directory

Create A public method which uses webview and pass String of ur String

public void setUrl(String urlString){
{
wv.loadUrl(urlString);
}

For this your WebView must have Global scope. Then only u can access it.
call anywhere like setUrl(path);

Best way to load a jpeg from an install-time asset pack and show it on a WebView

Perhaps the WebView is not the most optimal way to show a jpg in this case. I ended up using another control:

        com.ortiz.touchview.TouchImageView touchView = this.findViewById(R.id.touchImageView);
try {
AssetManager assetManager = getAssets();
InputStream is = assetManager.open(songNo + ".jpg");
Drawable d = Drawable.createFromStream(is,"src");
touchView.setImageDrawable(d);

}catch(Exception e){
Log.i("xxx", e.getMessage());
}


Related Topics



Leave a reply



Submit