How to display HTML images in textview

Source: Internet
Author: User

Extview is not only string-based. We usually call the Public final void settext (charsequence text) method when passing the string parameter to the settext () method, the string class is a subclass of charsequence.

There are many charsequence sub-classes. One of them is spanned, which is similar to HTML text with tags. We can use it to display HTML in textview (Naturally, there are many HTML tags that are not supported, and only some of them are supported ).


A Method of the android. Text. html class:

[Java]
View plaincopyprint?
  1. Public static spanned fromhtml (string source)

HTML code can be converted to spanned.

[Java]
View plaincopyprint?
  1. Html = "
  2. + "<P> This text is normal </P>"
  3. + " ";
  4. Spanned sp = html. fromhtml (HTML );
  5. Textview. settext (HTML );

Display Effect:

It can be seen that the font effect is basically displayed, but the image is not displayed.

To display images, another reconstruction method of HTML. fromhtml must be used:

Public static spanned fromhtml (string source, HTML. imagegetter, HTML. taghandler)

Here, HTML. imagegetter is an interface. We need to implement this interface. Only the drawable object of the image can be returned in its getdrawable (string source) method.

Modified code:

[Java]
View plaincopyprint?
  1. Spanned sp = html. fromhtml (HTML, new HTML. imagegetter (){
  2. @ Override
  3. Public drawable getdrawable (string source ){
  4. Inputstream is = NULL;
  5. Try {
  6. Is = (inputstream) new URL (Source). getcontent ();
  7. Drawable d = drawable. createfromstream (is, "src ");
  8. D. setbounds (0, 0, D. getintrinsicwidth (),
  9. D. getintrinsicheight ());
  10. Is. Close ();
  11. Return D;
  12. } Catch (exception e ){
  13. Return NULL;
  14. }
  15. }
  16. }, Null );
  17. Textview. settext (SP );

It seems complicated, but in fact, the second parameter of fromhtml () is an anonymous class used for obtaining images.

Where

[Java]
View plaincopyprint?
  1. Is = (inputstream) new URL (Source). getcontent ();
  2. Drawable d = drawable. createfromstream (is, "src ");

It is used to obtain the corresponding drawable instance through the image address.

Because images of network resources are used, you must add the following permissions to the mainifest file:

[HTML]
View plaincopyprint?
  1. <Uses-Permission Android: Name = "android. Permission. Internet"/>

Modified running result:


The image is displayed normally.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.