【问题标题】:Android webview is not displaying imageAndroid webview 不显示图像
【发布时间】:2017-01-30 19:51:02
【问题描述】:

我只是在学习如何使用 webview Android 显示一个 html 文件。

但包含在 html 文件中的图像在高于 kitkat 的 Android 版本上不显示。

我的代码是:

MainActivity.java inside oncreate

WebView web = (WebView)finditembyid(R.id.webview1);

Web.loadurl("file:///android_asset/index.html");

我的 html 文件包含:

  <img src="image.png">

在我的资产文件夹中:

Index.html
Image.png

我尝试通过以下方式更改图像标签上的 src:

Src="file:////android_asset/image.png
Src="/android/image.png"

但这些都不起作用。 我该怎么办?

【问题讨论】:

    标签: java android android-webview


    【解决方案1】:

    试试这个代码,它是一个示例代码,我在其中打开了包含超链接和图像的 webview 内容。 如果有链接,它会自动创建它,如果有图像标签,它会在 webview 中显示图像。

    String about_us_description_new = "<html><body style=color:black;>" + "<p align=\"justify\">" + about_us_description + "</p>" + "</body></html>";
                    webView2.setBackgroundColor(Color.TRANSPARENT);
                    webView2.getSettings().setJavaScriptEnabled(true);
                    webView2.loadDataWithBaseURL(null, about_us_description_new.toString(), "text/html", "utf-8", null);
    

    将您的内容替换为“about_us_description”,我已将内容存储在字符串变量中作为 about_us_description,然后完成了我的工作。

    希望对你有所帮助

    【讨论】:

    • HI Apoorv Mehrotra,他在资产文件夹中已经有 html 文件,那你为什么要再次创建 about_us_description_new
    • 我的文件是 html,但我可以使用 inputstream
    猜你喜欢
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多