【问题标题】:Android webview in web/website page use local asset file (help)网页/网站页面中的 Android webview 使用本地资产文件(帮助)
【发布时间】:2014-07-10 13:44:42
【问题描述】:

我无法在网站 html 页面中使用本地应用程序资产(示例脚本、图像)文件

示例:(webview)打开页面http://website.com/sample.html

还有这个sample.html

`<html><head><title>Error Internet</title>
</head><body>
<img src="file:///android_asset/image.png">
                    <h2><strong>&copy; 2014 -2015</strong></h2></body></html>

`

页面打开但本地文件未打开 file:///android_asset/image.png

【问题讨论】:

    标签: javascript android html webview assets


    【解决方案1】:

    只需删除 html 中的 file:///android_asset/, src="pathOfImage/image.png" 会做的。如果您从资产加载该 html。

    【讨论】:

      【解决方案2】:

      在 Android Studio 中,如果您没有 assets 文件夹,请创建一个。

      文件 - 新建 - 文件夹 - 资产文件夹

      将您的图像放在资产文件夹中。

      使用此代码从您的资产文件夹中加载带有图像的 webview:

      WebView web_view = findViewById(R.id.web_view_tts);
      String html = "<!DOCTYPE html>\n" +
              "<html>\n" +
              "    <head>\n" +
              "    </head>\n" +
              "    <body>\n" +
              "<img src=\"file:///android_asset/volume_up.png\">" +
              "    </body>\n" +
              "</html>";
      
      WebSettings webSettings = web_view.getSettings();
      
      web_view.setWebViewClient(new WebViewClient());
      webSettings.setJavaScriptEnabled(true); //use these settings for javascript
      webSettings.setAllowContentAccess(true);
      webSettings.setDomStorageEnabled(true);
      
      web_view.loadDataWithBaseURL("file:///android_asset", html, "text/html", "utf-8", null);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-06
        • 2021-02-19
        • 1970-01-01
        • 2017-11-10
        • 1970-01-01
        相关资源
        最近更新 更多