将 HTML 文件加载到 WebView 中

发布于 2024-11-02 22:16:07 字数 113 浏览 1 评论 0原文

我有一个本地 html 页面以及它指向的几个其他资源(css 文件和 Javascript 库),我想将它们加载到 WebView 中。如何才能实现这一目标?

也许这不是最好的方法,但我仍在尝试。

I have a local html page along with several other resources pointed by it (css files and Javascript libraries) that I would like to load into a WebView . How could this be achieved ?

Perhaps not the best way to procede but I'm still experimenting.

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

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

发布评论

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

评论(6

無心 2024-11-09 22:16:08

最简单的方法可能是将您的 Web 资源放入 assets 文件夹,然后调用:

webView.loadUrl("file:///android_asset/filename.html");

Java 和 Webview 之间的完整通信 查看此

更新:资产文件夹通常是以下文件夹:
<项目>/src/main/assets
可以在 .iml 文件 的资产文件夹配置设置中将其更改为:


请参阅文章 Android 中资源文件夹的放置位置工作室

The easiest way would probably be to put your web resources into the assets folder then call:

webView.loadUrl("file:///android_asset/filename.html");

For Complete Communication between Java and Webview See This

Update: The assets folder is usually the following folder:
<project>/src/main/assets
This can be changed in the asset folder configuration setting in your <app>.iml file as:

<option name=”ASSETS_FOLDER_RELATIVE_PATH” value=”/src/main/assets” />
See Article Where to place the assets folder in Android Studio

凉世弥音 2024-11-09 22:16:08

也许这个示例可以帮助:

  WebView lWebView = (WebView)findViewById(R.id.webView);
  File lFile = new File(Environment.getExternalStorageDirectory() + "<FOLDER_PATH_TO_FILE>/<FILE_NAME>");
  lWebView.loadUrl("file:///" + lFile.getAbsolutePath());

probably this sample could help:

  WebView lWebView = (WebView)findViewById(R.id.webView);
  File lFile = new File(Environment.getExternalStorageDirectory() + "<FOLDER_PATH_TO_FILE>/<FILE_NAME>");
  lWebView.loadUrl("file:///" + lFile.getAbsolutePath());
失而复得 2024-11-09 22:16:08

在这种情况下,使用 WebView#loadDataWithBaseUrl()WebView#loadUrl() 更好!

webView.loadDataWithBaseURL(url, 
        data,
        "text/html",
        "utf-8",
        null);

url: url/path 指向所有 JavaScript 文件和 html 链接都有其来源的目录的字符串。如果为空,则为:空白。
data:包含 hmtl 文件的字符串,例如使用 BufferedReader 读取

更多信息: WebView.loadDataWithBaseURL(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)

In this case, using WebView#loadDataWithBaseUrl() is better than WebView#loadUrl()!

webView.loadDataWithBaseURL(url, 
        data,
        "text/html",
        "utf-8",
        null);

url: url/path String pointing to the directory all your JavaScript files and html links have their origin. If null, it's about:blank.
data: String containing your hmtl file, read with BufferedReader for example

More info: WebView.loadDataWithBaseURL(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)

娇俏 2024-11-09 22:16:08

XML 布局文件:

<WebView android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/webView"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".activities.Bani9">
</WebView>

Java 代码:

public class Bani9 extends AppCompatActivity {
    WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bani9);
        webView = findViewById(R.id.webView);
        WebSettings webSetting = webView.getSettings();
        webSetting.setBuiltInZoomControls(true);
        webView.setWebViewClient(new WebViewClient());

        webView.loadUrl("file:///android_asset/punjabi/bani9.html");
    }
}

确保准确设置文件路径。
确保您正确设置文件路径

XML Layout File:

<WebView android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/webView"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".activities.Bani9">
</WebView>

Java Code:

public class Bani9 extends AppCompatActivity {
    WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bani9);
        webView = findViewById(R.id.webView);
        WebSettings webSetting = webView.getSettings();
        webSetting.setBuiltInZoomControls(true);
        webView.setWebViewClient(new WebViewClient());

        webView.loadUrl("file:///android_asset/punjabi/bani9.html");
    }
}

Make sure you set file path accurately.
Make sure you set File Path properly

疧_╮線 2024-11-09 22:16:08

来自官方指南 https://developer.android。 com/develop/ui/views/layout/webapps/load-local-content

  1. 将 HTML 作为资源存储在 app/src/main/assets/
  2. 使用 WebViewAssetLoader 加载资源。在 onCreate() 中构造它,如下所示:
final WebViewAssetLoader assetLoader = new WebViewAssetLoader.Builder()
         .addPathHandler("/assets/", new WebViewAssetLoader.AssetsPathHandler(this))
         .addPathHandler("/res/", new WebViewAssetLoader.ResourcesPathHandler(this))
         .build();
  1. 子类 WebViewClient 包装 WebViewAssetLoader
private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient(WebViewAssetLoader assetLoader) {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi(21)
    public WebResourceResponse shouldInterceptRequest(WebView view,
                                     WebResourceRequest request) {
        return mAssetLoader.shouldInterceptRequest(request.getUrl());
    }

    @Override
    @SuppressWarnings("deprecation") // to support API < 21
    public WebResourceResponse shouldInterceptRequest(WebView view,
                                     String url) {
        return mAssetLoader.shouldInterceptRequest(Uri.parse(url));
    }
}

这基本上将请求 URL 传递给 WebViewAssetLoader 以从资产加载 Web 内容。

  1. 使用(2)中的assetLoader构建(3)中的WebViewClient,并将其设置在您的WebView中。您的 index.html 可以使用 https 和默认域 appassets.androidplatform.net 加载:
mWebView.setWebViewClient(new LocalContentWebViewClient(assetLoader));
mWebView.loadUrl("https://appassets.androidplatform.net/assets/index.html");

请注意,使用类似 Web 的 URL 而不是 file 加载本地文件:// 是理想的,因为它与同源策略兼容。

From the official guide https://developer.android.com/develop/ui/views/layout/webapps/load-local-content :

  1. Store the HTML as an asset in app/src/main/assets/
  2. Use WebViewAssetLoader to load the asset. Construct it in your onCreate() as follows:
final WebViewAssetLoader assetLoader = new WebViewAssetLoader.Builder()
         .addPathHandler("/assets/", new WebViewAssetLoader.AssetsPathHandler(this))
         .addPathHandler("/res/", new WebViewAssetLoader.ResourcesPathHandler(this))
         .build();
  1. Subclass WebViewClient to wrap WebViewAssetLoader:
private static class LocalContentWebViewClient extends WebViewClientCompat {

    private final WebViewAssetLoader mAssetLoader;

    LocalContentWebViewClient(WebViewAssetLoader assetLoader) {
        mAssetLoader = assetLoader;
    }

    @Override
    @RequiresApi(21)
    public WebResourceResponse shouldInterceptRequest(WebView view,
                                     WebResourceRequest request) {
        return mAssetLoader.shouldInterceptRequest(request.getUrl());
    }

    @Override
    @SuppressWarnings("deprecation") // to support API < 21
    public WebResourceResponse shouldInterceptRequest(WebView view,
                                     String url) {
        return mAssetLoader.shouldInterceptRequest(Uri.parse(url));
    }
}

This basically passes the request URL to WebViewAssetLoader to load web content from an asset.

  1. Use assetLoader from (2) to construct WebViewClient from (3), and set it in your WebView. Your index.html can be loaded by using https and the default domain appassets.androidplatform.net:
mWebView.setWebViewClient(new LocalContentWebViewClient(assetLoader));
mWebView.loadUrl("https://appassets.androidplatform.net/assets/index.html");

Note that loading local files using web-like URLs instead of file:// is desirable as it is compatible with the Same-Origin policy.

森罗 2024-11-09 22:16:08

接受的答案对我不起作用,这对我有用

WebSettings webSetting = webView.getSettings();
    webSetting.setBuiltInZoomControls(true);
    webView1.setWebViewClient(new WebViewClient());

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

The Accepted Answer is not working for me, This is what works for me

WebSettings webSetting = webView.getSettings();
    webSetting.setBuiltInZoomControls(true);
    webView1.setWebViewClient(new WebViewClient());

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