Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying Android asset files in a WebView?

Tags:

I have seen various discussions on the problem of serving WebView pages from assets, none of which seemed definitive.

I want to be able to use a webview to display html (and included css) files stored in the project assets.

I have found that wv.loadUrl("file:///android_asset/html_no_copy/demo_welcome.html") displays that file okay, but links in demo_welcome.html, either local (no url prefixing the file name) or absolute - the same form as fed to loadUrl - don't work. They get a "Web page not available" error displayed on the view.

WebView wv = (WebView)this.findViewById(R.id.splashWebView); wv.loadUrl("file:///android_asset/html_no_copy/test.html"); // Works 

or

wv.loadUrl("file:///android_asset/html_no_copy/demo_welcome.html"); // Works 

But neither of the following links in demo_welcome.html work:

<a href="test.html">CLICK HERE</a><p> <a href="file:///android_asset/html_no_copy/test.html">OR HERE</a> 

I know I can get around this by writing a content provider, but that seems extreme.

I want this to work from SDK 1.6 (4) on up.

Does anyone know if this can be done with just HTML, or does one need to kluge up some code to load the data?

like image 244
Mesocyclone Avatar asked Mar 16 '11 02:03

Mesocyclone


People also ask

How do I access Android assets?

Step 3 – Right click app >> New >> Folder >> Assets folder. Right click on the assets folder, select New >> file (myText. txt) and your text.

How do I display HTML content in WebView?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken web view to show html content.

How do I view PDF in Android WebView?

The very first and the easiest way of displaying the PDF file is to display it in the WebView. All you need to do is just put WebView in your layout and load the desired URL by using the webView. loadUrl() function. Now, run the application on your mobile phone and the PDF will be displayed on the screen.


1 Answers

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> 
like image 198
Mesocyclone Avatar answered Nov 09 '22 11:11

Mesocyclone