使用 android WebView 加载现有的 .html 文件

发布于 2024-09-29 14:40:43 字数 684 浏览 1 评论 0原文

我确实使用 WebView 尝试了来自 Google 代码和其他资源的示例、演示,但是当我尝试在自己的代码中执行此操作时,它对我不起作用。

我想加载我放在资产文件夹中的 myfile.html ,并使用:

private WebView myWebView;

myWebView.loadUrl("file:///android_assets/myfile.html");

在模拟器上显示错误

无法访问 file:///android_assets/myfile.html 的网页 加载为:找不到请求的文件。 /android_assets/myfile.html

当我将该文件放入 res/raw/ 文件夹并使用:

myWebView.loadUrl("file:///android_res/raw/myfile.html");

那么只有模拟器 android 2.2 API level 8 可能可以加载该文件,其他较旧的文件版本显示相同的错误。我错过了什么吗?

有没有什么方法可以加载适用于所有 API 版本的应用程序包中的现有 .html 文件?

I did try samples, demos from Google codes and other resources with WebView, but when i try to do it in my own code, it doesn't work for me.

I want to load myfile.html which i put in assets folder, and using:

private WebView myWebView;

myWebView.loadUrl("file:///android_assets/myfile.html");

On emulator shows error

The web page at file:///android_assets/myfile.html could not be
loaded as: The requested file was not found.
/android_assets/myfile.html

When i put that file to res/raw/ folder and using:

myWebView.loadUrl("file:///android_res/raw/myfile.html");

then only emulator android 2.2 API level 8 can load the file probably, other older versions show the same error. Am i missing something?

Is there any way of loading an existing .html file in the application package which works on all API versions ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

无语# 2024-10-06 14:40:44

好吧,那是我非常愚蠢的错误。我将答案发布在这里,以防万一有人遇到同样的问题。

存储在资产文件夹中的文件的正确路径是 file:///android_asset/* (资产文件夹没有“s”,我一直认为它必须有一个“s”)。

并且,mWebView.loadUrl("file:///android_asset/myfile.html");适用于所有 API 级别。

我仍然不明白为什么 mWebView.loadUrl("file:///android_res/raw/myfile.html"); 仅适用于 API 级别 8。但现在已经不重要了。

ok, that was my very stupid mistake. I post the answer here just in case someone has the same problem.

The correct path for files stored in assets folder is file:///android_asset/* (with no "s" for assets folder which i was always thinking it must have a "s").

And, mWebView.loadUrl("file:///android_asset/myfile.html"); works under all API levels.

I still not figure out why mWebView.loadUrl("file:///android_res/raw/myfile.html"); works only on API level 8. But it doesn't matter now.

哥,最终变帅啦 2024-10-06 14:40:44

如果你的结构应该是这样的:

/assets/html/index.html

/assets/scripts/index.js

/assets/css/index.css< /code>

然后就做( Android WebView:处理方向更改

    if(WebViewStateHolder.INSTANCE.getBundle() == null) { //this works only on single instance of webview, use a map with TAG if you need more
        webView.loadUrl("file:///android_asset/html/index.html");
    } else {
        webView.restoreState(WebViewStateHolder.INSTANCE.getBundle());
    }

确保添加

    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);
    }

然后只使用 url

<html>
<head>
    <meta charset="utf-8">
    <title>Zzzz</title>
    <script src="../scripts/index.js"></script>
    <link rel="stylesheet" type="text/css" href="../css/index.css">

If your structure should be like this:

/assets/html/index.html

/assets/scripts/index.js

/assets/css/index.css

Then just do ( Android WebView: handling orientation changes )

    if(WebViewStateHolder.INSTANCE.getBundle() == null) { //this works only on single instance of webview, use a map with TAG if you need more
        webView.loadUrl("file:///android_asset/html/index.html");
    } else {
        webView.restoreState(WebViewStateHolder.INSTANCE.getBundle());
    }

Make sure you add

    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);
    }

Then just use urls

<html>
<head>
    <meta charset="utf-8">
    <title>Zzzz</title>
    <script src="../scripts/index.js"></script>
    <link rel="stylesheet" type="text/css" href="../css/index.css">
无畏 2024-10-06 14:40:44

将 .html 文件粘贴到项目文件夹的 asset 文件夹中。
并使用以下代码在布局文件夹中创建一个 xml 文件:
my.xml:

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/webview"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
    />

在活动中添加以下代码

setContentView(R.layout.my);
    WebView mWebView = null;
    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.loadUrl("file:///android_asset/new.html"); //new.html is html file name.

paste your .html file in assets folder of your project folder.
and create an xml file in layout folder with the fol code:
my.xml:

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/webview"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
    />

add fol code in activity

setContentView(R.layout.my);
    WebView mWebView = null;
    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.loadUrl("file:///android_asset/new.html"); //new.html is html file name.
清音悠歌 2024-10-06 14:40:44

将 .html 文件复制并粘贴到项目的 asset 文件夹中,并在 onCreate() 的 Activity 中添加以下代码。

        WebView view = new WebView(this);
        view.getSettings().setJavaScriptEnabled(true);
        view.loadUrl("file:///android_asset/**YOUR FILE NAME**.html");
        view.setBackgroundColor(Color.TRANSPARENT);
        setContentView(view);

Copy and Paste Your .html file in the assets folder of your Project and add below code in your Activity on onCreate().

        WebView view = new WebView(this);
        view.getSettings().setJavaScriptEnabled(true);
        view.loadUrl("file:///android_asset/**YOUR FILE NAME**.html");
        view.setBackgroundColor(Color.TRANSPARENT);
        setContentView(view);
埖埖迣鎅 2024-10-06 14:40:44

调试编译发布编译不同,因此:

请考虑您的项目文件结构,如下所示[这种情况是针对调试汇编]:

src
  |
  debug
      |
      assets
           |
           index.html

您应该将 index.html 调用到您的 WebView 中,如下所示:

web.loadUrl("file:///android_asset/index.html");

因此,对于发布组装,它应该类似于:

src
  |
  release
        |
        assets
             |
             index.html

下面的结构也适用于两种编译 [调试和发布]:

src
  |
  main
     |
     assets
          |
          index.html

The debug compilation is different from the release one, so:

Consider your Project file structure like that [this case if for a Debug assemble]:

src
  |
  debug
      |
      assets
           |
           index.html

You should call index.html into your WebView like:

web.loadUrl("file:///android_asset/index.html");

So forth, for the Release assemble, it should be like:

src
  |
  release
        |
        assets
             |
             index.html

The bellow structure also works, for both compilations [debug and release]:

src
  |
  main
     |
     assets
          |
          index.html
终陌 2024-10-06 14:40:44

您可以手动读取 html 文件,然后使用 WebView 的 loadDataloadDataWithBaseUrl 方法来显示它。

You could read the html file manually and then use loadData or loadDataWithBaseUrl methods of WebView to show it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文