2

I am parsing a json feed and displaying its content in a android webview. Everything works great. But now i want to hide all img tags in that android webview.

The problem is that the content in the webview is showen dynamically, that means i don't know the img tag parameters. So i need something to replace everything in that string that begins with

     <img ...  > 

and ends with

   </img>

How can I do that?

2 Answers 2

7

Because none of these answeres worked for me, i finally decided to use jsoup to filter and remove all img tags.

using jsop was a little bit complex but it worked!!

EDIT Here is an example java code

String content = "<h1>title</h1><img src="http://..."></img>";
if(content != null) {
    Document document = Jsoup.parse(content);
    document.select("img").remove();
    content = document.toString();
}
Sign up to request clarification or add additional context in comments.

4 Comments

How do you remove image tags using jsoup
@TasawerKhan its esay, if you want i can edit the answer with a working example
I found the solution. But if you can that will be helpful to others.
Great!. I used document.html()
1

If you handle the String yourself and you set it to the webview, then suppose your content is in a String called oldWebViewContent , and try this :

String webViewContentExcludeImage = oldWebViewContent.replaceAll("<img .*?</img>","");

8 Comments

Thanks, but didn't worked. Any other solution maybe?
try to remove \n from your string : oldWebViewContent=oldWebViewContent.replaceAll("\\n",""); then use the code in the answer.
There's always the possibility of having something like <img src=""/>, which would not work for this example. In fact, an img tag may not even have a / at all. Due to the nature of HTML, regex is not usually the best thing to use.
if that is the case, use two replaceAll calls. one as stated above and one with ("<img.*/>", "")
What if it doesn't have a close (/) tag at all? (As used on the W3C site)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.