android java get html image tag from string

后端 未结 4 568
深忆病人
深忆病人 2020-12-10 07:33

Im trying to get HTML image tag url from the given string. There should be some regular expression to get it. But don\'t know how to do it. Can anyone help me on this.

相关标签:
4条回答
  • 2020-12-10 07:36

    Frist of All Import jsoap:

    compile group: 'org.jsoup', name: 'jsoup', version: '1.7.2'
    

    Then you can Use this:

    private ArrayList pullLinks(String html) {
        ArrayList links = new ArrayList();
        Elements srcs = Jsoup.parse(html).select("[src]"); //get All tags containing "src"
        for (int i = 0; i < srcs.size(); i++) {
            links.add(srcs.get(i).attr("abs:src")); // get links of selected tags
        }
        return links;
    }
    
    0 讨论(0)
  • 2020-12-10 07:37

    Please see this question for reference. Basically it says to use:

    String imgRegex = "<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>";
    
    0 讨论(0)
  • 2020-12-10 07:51

    An XMLPullParser can do this pretty easily. Although, if it is a trivially small string, it may be overkill.

         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
         XmlPullParser xpp = factory.newPullParser();
    
         xpp.setInput( new StringReader ( "<html>I have string like this with <br> some HTML<b>tag</b> with <img src=\"http://xyz.com/par.jpg\" align=\"left\"/> image tags in it. how can get it ?</html>" ) );
         int eventType = xpp.getEventType();
         while (eventType != XmlPullParser.END_DOCUMENT) {
          if(eventType == XmlPullParser.START_TAG && "img".equals(xpp.getName()) {
              //found an image start tag, extract the attribute 'src' from here...
          }
          eventType = xpp.next();
         }
    
    0 讨论(0)
  • 2020-12-10 07:53

    I use jsoup. It is pretty easy to use and lightweight. Some versions were not Java 1.5 compatible but it appears they fixed the issue.

    String html = str;
    Document doc = Jsoup.parse(html);
    Elements pngs = doc.select("img[src$=.png]"); // img with src ending .png
    
    0 讨论(0)
提交回复
热议问题