【问题标题】:How to get image tag from html code into Glide as source for image in Java?如何从 html 代码中获取图像标签到 Glide 作为 Java 中的图像源?
【发布时间】:2019-12-20 08:50:49
【问题描述】:

我找到了一些从 html img 标签中提取图片链接的代码。它使用 Html Java 库并使用来自 Html 的某些方法。

我从一些 RSS 提要中提取了 html 代码,我想提取并显示图像。我已经完成了下面的代码,但我认为这根本不是优雅的解决方案:

Html.fromHtml(html, new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(final String source) {
                Drawable d = null;
                Glide.with(mContext)
                        .load(source)
                        .into(img);
                sImgUrl = source;
                return d;
            }
        }, null);

是否可以在不覆盖 getDrawable() 的情况下提取此“源”字符串?

【问题讨论】:

    标签: java android html


    【解决方案1】:

    您可以像这样使用 JSoup Lib 手动完成。

    String html = "<html>your html code goes here</html>";
    
    Document doc = Jsoup.parse(html);
    Elements image = doc.getElementsByTag("img");
    
     for (Element el : image) {
       String src = el.absUrl("src");
       System.out.println("src attribute is : "+src);
     }
    

    用滑行做你的事

    这是参考answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-23
      • 2018-02-15
      • 1970-01-01
      • 2017-04-26
      • 2017-03-21
      相关资源
      最近更新 更多