Displaying Android asset files in a WebView?
Well, I found something that seems to work (on 1.6 and 2.2), in spite of a warning that it would recurse.
I also discovered that a css style-sheet link inside the first and second page both work without the following intercept. Odd and it makes me a bit nervous. Thoughts?
Here's the code:
WebView wv = (WebView)this.findViewById(R.id.splashWebView);
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
wv.loadUrl("file:///android_asset/html_no_copy/demo_welcome.html");
Here's the file contents:
demo_welcome.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Demo Html</title>
<link rel="stylesheet" type="text/css" href="demo.css" />
</head>
<body>
<H1>Testing One Two Three</H1>
<a href="test.html">CLICK HERE</a><p>
<a href="file:///android_asset/html_no_copy/test.html">OR HERE</a>
</body>
</html>
test.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="test.css" />
<title>Insert title here</title>
</head>
<body>
<H1>TEST.HTML</H1>
</body>
</html>
Images in Android assets subfolder do not display in WebView
After much frustration, I finally found out that I couldn't view this particular image on any Android device, even from the stock browser, despite it being what I thought was a normal JPG image.
I'm still not sure what was wrong with it, but when I opened it up in my photo editor and saved it again with normal JPG encoding, it worked. That includes when I reference it from the assets folder in my WebView.
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:
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);
Related Topics
Searchview in Optionsmenu Not Full Width
Error When Using Any Android Design Support Library Elements
Android:Google Maps API Key Signup:Md5 Certification Key
How to Change the Group Owner in a Persistent Group in Wi-Fi Direct
Given a Tensor Flow Model Graph, How to Find the Input Node and Output Node Names
How to Access an Image from the Phone's Photo Gallery
Why Is Calling Process.Killprocess(Process.Mypid()) a Bad Idea
Android Audiotrack Playing .Wav File, Getting Only White Noise
How to Download a Video File to Sd Card
How to Delete from Firebase Realtime Database
Access_Coarse_Location Permission Gives a Cell Tower Precision on Android
Android: How to Update a Imageview/Imagebutton with a Number to Show the Number of New Messages
Dagger 2 Injecting Android Application Context
Android - Periodic Background Service - Advice
Mediaplayer.Setdatasource() and Prepare() Not Working - Android
Firebase Authentication State Change Does Not Fire When User Is Disabled or Deleted