How to Display Inline Images from HTML in an Android Textview

Is it possible to display inline images from html in an Android TextView?

If you have a look at the documentation for Html.fromHtml(text) you'll see it says:

Any <img> tags in the HTML will display as a generic replacement image which your program can then go through and replace with real images.

If you don't want to do this replacement yourself you can use the other Html.fromHtml() method which takes an Html.TagHandler and an Html.ImageGetter as arguments as well as the text to parse.

In your case you could parse null as for the Html.TagHandler but you'd need to implement your own Html.ImageGetter as there isn't a default implementation.

However, the problem you're going to have is that the Html.ImageGetter needs to run synchronously and if you're downloading images from the web you'll probably want to do that asynchronously. If you can add any images you want to display as resources in your application the your ImageGetter implementation becomes a lot simpler. You could get away with something like:

private class ImageGetter implements Html.ImageGetter {

public Drawable getDrawable(String source) {
int id;

if (source.equals("stack.jpg")) {
id = R.drawable.stack;
}
else if (source.equals("overflow.jpg")) {
id = R.drawable.overflow;
}
else {
return null;
}

Drawable d = getResources().getDrawable(id);
d.setBounds(0,0,d.getIntrinsicWidth(),d.getIntrinsicHeight());
return d;
}
};

You'd probably want to figure out something smarter for mapping source strings to resource IDs though.

How to display HTML img tag inside Android TextView

You should use WebView instead of TextView

   WebView = findViewById(R.id.WebView);
WebView.loadData(source, "text/html", "utf-8");

You will get the same output.

Show image from HTML tag in text view?

You can use this code for set html image and text both from textview:

This is xml code:

      <TextView
android:id="@+id/txtDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:lineSpacingExtra="5dp"
android:padding="5dp"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="@color/black" />

This is Java code:

public class ConditionOfUseActivity extends Activity implements Html.ImageGetter {
private TextView txtDescription;
private Drawable empty;

@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_condition_use);
txtDescription = (TextView) findViewById(R.id.txtDescription);
String source = "this is a test of <b>ImageGetter</b> it contains " +
"two images: <br/>" +
"<img src=\"http://developer.android.com/assets/images/dac_logo.png\"><br/>and<br/>" +
"<img src=\"http://developer.android.com/assets/images/icon_search.png\">";
Spanned spanned = Html.fromHtml(object.getString("content"), ConditionOfUseActivity.this, null);
txtDescription.setText(spanned);
}

@Override
public Drawable getDrawable(String s) {
LevelListDrawable d = new LevelListDrawable();
empty = getResources().getDrawable(R.drawable.splash1);
d.addLevel(0, 0, empty);
d.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());
new LoadImage().execute(s, d);
return d;
}

class LoadImage extends AsyncTask<Object, Void, Bitmap> {
private LevelListDrawable mDrawable;

@Override
protected Bitmap doInBackground(Object... params) {
String source = (String) params[0];
mDrawable = (LevelListDrawable) params[1];
Log.d(TAG, "doInBackground " + source);
try {
InputStream is = new URL(source).openStream();
return BitmapFactory.decodeStream(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null) {
BitmapDrawable d = new BitmapDrawable(bitmap);
mDrawable.addLevel(1, 1, d);
//mDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
mDrawable.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());
mDrawable.setLevel(1);
CharSequence t = txtDescription.getText();
txtDescription.setText(t);
}
}
}

Visualizing an image from Html.fromHtml in a TextView(The image must be in the server)

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.

How to save/read inline images in html content to internal/extrenal memory

You need to implement picasso cache by your own. This should be like this:

public class PicassoCache {

private static Picasso picassoInstance = null;

private PicassoCache(Context context) {

Downloader downloader = new OkHttp3Downloader(context, Integer.MAX_VALUE);
Picasso.Builder builder = new Picasso.Builder(context);
builder.downloader(downloader);

picassoInstance = builder.build();
}

public static Picasso getPicassoInstance(Context context) {

if (picassoInstance == null) {

new PicassoCache(context);
return picassoInstance;
}

return picassoInstance;
}
}

Then in your code:

    @Override
public Drawable getDrawable(String source) {

BitmapDrawablePlaceHolder drawable = new BitmapDrawablePlaceHolder();
PicassoCache.getPicassoInstance(getContext()).load(source).into(drawable);
return drawable;

}

OkHttp3Downloader will install an image cache into your application cache directory. You can also use another constructor with your own provided directory public OkHttp3Downloader(final File cacheDir, final long maxSize)

As an alternative, you can use Glide. This is like picasso but also allows you to show gifs with animation and cache strategy IMHO is a bit easier.

Glide.with(context)
.load(source)
.apply(RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL))
.into(drawable)

image is display on android textview using html.fromHtml


public static Spanned fromHtml (String source)

Added in API level 1
Returns displayable styled text from the provided HTML string. Any tags in the HTML will display as a generic replacement image which your program can then go through and replace with real images.

This uses TagSoup to handle real HTML, including all of the brokenness found in the wild.

so insted of that use

public static Spanned fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)

Returns displayable styled text from the provided HTML string. Any tags in the HTML will use the specified ImageGetter to request a representation of the image (use null if you don't want this) and the specified TagHandler to handle unknown tags (specify null if you don't want this).

This uses TagSoup to handle real HTML, including all of the brokenness found in the wild.

for more detail check this answer;

Inline images with Text in android

I figured it out. Instead of:

Spanned span = Html.fromHtml(contents[1], mig, null);
contents[1] = span.toString();

content.setText(contents[1]);

I need:

Spanned span = Html.fromHtml(contents[1], mig, null);
content.setText(span);

I was running into problems setting span to string.

Show image in textview using Html.fromhtml();

You can draw an Image in TextView using the Html img tag with Html.ImageGetter. But make sure your image is available in resource drawable folder

Here is a sample , The image will be loaded from the resource.

String htmlText = "Hai <img src=\"ic_launcher\"> Hello";

textView.setText(Html.fromHtml(htmlText, new Html.ImageGetter() {

@Override
public Drawable getDrawable(String source) {
int resourceId = getResources().getIdentifier(source, "drawable",getPackageName());
Drawable drawable = getResources().getDrawable(resourceId);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
return drawable;
}
}, null));

Inline images in a TextView

Instead of use HTML, you can use

labelVotes.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.icon_upvotes);

and playing a little bit with padding, using setCompoundDrawablePadding



Related Topics



Leave a reply



Submit