【问题标题】:How to display HTML images along with the text in android.?如何在android中显示HTML图像和文本。?
【发布时间】:2015-06-29 16:12:04
【问题描述】:

我是安卓开发新手。 我正在使用 webview 在 android 中显示 HTML 页面,但只显示文本。 你能帮我解决这个问题吗? 提前谢谢你,会很有帮助的。

 WebView view = (WebView) this.findViewById(R.id.webView);
    try{

        InputStream stream = this.getAssets().open("Capacitor Code Calculator.html");
        int streamsize = stream.available();

        byte[] buffer = new byte[streamsize];
        stream.read(buffer);
        stream.close();
        String html = new String(buffer);
        view.loadData(html,"text/html", "utf-8");
    }
    catch (Exception e){
        e.printStackTrace();

是否必须对 view.loadData(html,"text/html", "utf-8"); 中的 "text/html" 做一些事情?

我的 html 和图片文件都在 assets 文件夹中。

【问题讨论】:

标签: android html image android-webview


【解决方案1】:

可能是你的文件名中不能有空格:

InputStream stream = this.getAssets().open("Capacitor Code Calculator.html");

您在<img> 标签中提供的网址是什么?你这个名字也有空格吗?

【讨论】:

  • HTML 文件正确打开,它显示文本原样......但如果是图像,它会给出一个小方块:( 对于图像 URL,我复制了资产文件夹中文件的整个资产路径
  • 图片的网址是什么样的?
【解决方案2】:

WebView 视图 = (WebView) this.findViewById(R.id.webview);

    view.getSettings().setJavaScriptEnabled(true);
    view.loadUrl(url);
    view.setWebViewClient(new WebViewClient());

最后的 setWebViewClient() 显示图像.....终于松了一口气:) 希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多