Can Android's WebView automatically resize huge images?

前端 未结 8 1839
心在旅途
心在旅途 2020-11-28 05:41

In some web browsers, huge images are automatically resized to fit the screen.

Is it possible to do the same in an Android WebView?

The web page just contain

8条回答
  •  死守一世寂寞
    2020-11-28 06:24

    I faced the same problem and used Jsoup to help me out and add the required respected CSS. You can easily add attributes or CSS. I my case, I download from many sources various different HTML files, save them and then display them in a Webview. Here is how I parse the HTML before I save it to the database with Kotlin:

    // Parse your HTML file or String with Jsoup
    val doc = Jsoup.parse("MY HTML STRING")
    
    // doc.select selects all tags in the the HTML document
    doc.select("img").attr("width", "100%") // find all images and set with to 100%
    doc.select("figure").attr("style", "width: 80%") // find all figures and set with to 80%
    doc.select("iframe").attr("style", "width: 100%") // find all iframes and set with to 100%
    // add more attributes or CSS to other HTML tags
    
    val updatedHTMLString = doc.html()
    // save to database or load it in your WebView
    

    Add Jsoup to your project

    Have fun!

提交回复
热议问题