在 Android WebView 上拟合图像

Fitting Image on Android WebView

我正在使用 WebView,我想在其中显示图像。

我想在 webview 中调整图像。 (图片变大了,只能水平滚动)

我展示这样的图片(如果重要的话):<img src="path" alt="">

我尝试过的:

WebView content = (WebView) findViewById(R.id.webView1);
content.loadDataWithBaseURL(null, "<style>img{display: inline;height: auto;max-width: 100%;} 
</style>" + CONTENT, "text/html", "UTF-8", null);

但是当我使用这种方法时,图像不可见。仅文本

我也试过这个:

   WebSettings settings = preview.getSettings();
   settings.setLoadWithOverviewMode(true);
   settings.setBuiltInZoomControls(true);

它可以工作,但效果不佳,因为它会缩小 webview 并且文本变得不可读。 还有其他办法吗?

如果您使用此代码,它将允许您在网络视图中显示所有图像

String imageUrl = "<Put here the image>";
        String html = "<html><head>" +
                "<meta name=\"viewport\" content=\"width=device-width, user-scalable=no\">"+
                "<style>\n" +
                "html,body{ margin:auto;}"+
                "#image{\n" +
                "    width: 100vw;\n" +
                "    height: 100vh;\n" +
                "}\n" +
                "</style></head>\n" +
                "<body>\n" +
                "<img id=\"image\" src=\""+imageUrl+"\" alt =\"no image\">\n" +
                "</body></html>";
        webView.loadDataWithBaseURL(null,html,"text/html", "UTF-8", null);