显示Android的资源文件中的WebView?文件、资源、Android、WebView

2023-09-12 01:16:07 作者:幸福已停机

我看到的服务,从资产,其中没有一个似乎明确的WebView网页的问题,各种讨论。

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

我希望能够使用的WebView显示HTML(并包括CSS)存储在项目资产的文件。

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

我已发现,wv.loadUrl(文件:///android_asset/html_no_copy/demo_welcome.html),该文件还行显示,但在demo_welcome.html链接,无论是本地(没有链接prefixing的文件名)或绝对 - 作为送入使用loadURL相同的形式 - 不工作。他们得到一个找不到网页在视图中显示错误。

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

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

不过,无论是在demo_welcome.html工作以下链接:

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.

我想这从SDK 1.6(4)后续工作。

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

有谁知道这是否可以只用HTML来完成,或者做一件需要克鲁格一些code加载数据?

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

推荐答案

嗯,我发现的东西,似乎工作(在1.6和2.2),尽管有警告,这将递归的。

Well, I found something that seems to work (on 1.6 and 2.2), in spite of a warning that it would recurse.

我还发现,在第一和第二页内的CSS样式表链路没有下面的截距两者工作。奇怪,这让我有点紧张。思考?

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?

这里的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");

下面是该文件的内容:

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>
 
精彩推荐
图片推荐