0

I have this problem. I'm trying to show an image in a TextView in Android Studio with the method Html.fromHtml, but the problem is the image is in the server, so how can I do this? There is the code:

TextView1.setText(Html.fromHtml(s), TextView.BufferType.SPANNABLE);
TextView1.setMovementMethod(LinkMovementMethod.getInstance());

PS: The image is in the server because I use the HttpUrlConnection for get the HTML TEXT from Internet

Tom11
  • 2,419
  • 8
  • 30
  • 56
Elia D'Amato
  • 35
  • 1
  • 1
  • 8

1 Answers1

0

The method Html.fromHtml supports loading of <img> tags, however you're going to need to manage the way the images are fetched yourself.

Remember, this is not a web client... this is an Android app. The fromHtml method is really a method which interprets some html tags, and creates appropriate Spannables which best fit the html tags.

And so, we're back the the issue with the images (img tags). Again, this is not a web client. You're going to need to load the images yourself.

To do this, you need to pass to the fromHtml method an ImageGetter implementing class. See example for how to implement an ImageGetter here.

Short version - you'll need to implement a method which accepts a string of the asset name, and returns a Drawable object. This method needs to be synchronous, which means you're going to need to pre-load all the image files from the server before-hand and have them ready to load when you call fromHtml.

Community
  • 1
  • 1
Gil Moshayof
  • 16,633
  • 4
  • 47
  • 58