Html.Imagegetter

Html.ImageGetter TextView

the easiest solution is:

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.pskink.soom.R;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LevelListDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.Spanned;
import android.util.Log;
import android.widget.TextView;

public class TestImageGetter extends Activity implements ImageGetter {
private final static String TAG = "TestImageGetter";
private TextView mTv;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_image_getter);
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(source, this, null);
mTv = (TextView) findViewById(R.id.text);
mTv.setText(spanned);
}

@Override
public Drawable getDrawable(String source) {
LevelListDrawable d = new LevelListDrawable();
Drawable empty = getResources().getDrawable(R.drawable.ic_launcher);
d.addLevel(0, 0, empty);
d.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());

new LoadImage().execute(source, 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) {
Log.d(TAG, "onPostExecute drawable " + mDrawable);
Log.d(TAG, "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.setLevel(1);
// i don't know yet a better way to refresh TextView
// mTv.invalidate() doesn't work as expected
CharSequence t = mTv.getText();
mTv.setText(t);
}
}
}
}

there is one not quite elegant way to re-layout a TextView after image downloading:

// i don't know yet a better way to refresh TextView
// mTv.invalidate() doesn't work as expected
CharSequence t = mTv.getText();
mTv.setText(t);

if anybody knows better solution please let me know

Html.ImageGetter

To get images from the application resources first in the text file one inserts an html image tag like this:

<img src="my_image">

Note that "my_image" is just a name of a drawable not a path. Then use this code to diplay the text with images in TextView

myTextView.setText(Html.fromHtml(myText, new ImageGetter() {
@Override public Drawable getDrawable(String source) {
Drawable drawFromPath;
int path =
myActivity.this.getResources().getIdentifier(source, "drawable",
"com.package...");
drawFromPath = myActivity.this.getResources().getDrawable(path);
drawFromPath.setBounds(0, 0, drawFromPath.getIntrinsicWidth(),
drawFromPath.getIntrinsicHeight());
return drawFromPath;
}
}, null));

If the source in the img tag is misspelled the applicaiton will crash because the method will fail to find the drawable, so more code can be added to prevent this...

Android HTML ImageGetter as AsyncTask

I've done something very similar (I think) to what you want to do. What I needed to do back then is parse the HTML and set it up back to TextView and I needed to use Html.ImageGetter as well and having the same problem on fetching image on the main thread.

The steps that I did basically:

  • Create my own subclass for Drawable to facilitate redraw, I called it URLDrawable
  • Return the URLDrawable in getDrawable method of Html.ImageGetter
  • Once onPostExecute is called, I redraw the container of the Spanned result

Now the code for URLDrawable is as follow


public class URLDrawable extends BitmapDrawable {
// the drawable that you need to set, you could set the initial drawing
// with the loading image if you need to
protected Drawable drawable;

@Override
public void draw(Canvas canvas) {
// override the draw to facilitate refresh function later
if(drawable != null) {
drawable.draw(canvas);
}
}
}

Simple enough, I just override draw so it would pick the Drawable that I set over there after AsyncTask finishes.

The following class is the implementation of Html.ImageGetter and the one that fetches the image from AsyncTask and update the image

public class URLImageParser implements ImageGetter {
Context c;
View container;

/***
* Construct the URLImageParser which will execute AsyncTask and refresh the container
* @param t
* @param c
*/
public URLImageParser(View t, Context c) {
this.c = c;
this.container = t;
}

public Drawable getDrawable(String source) {
URLDrawable urlDrawable = new URLDrawable();

// get the actual source
ImageGetterAsyncTask asyncTask =
new ImageGetterAsyncTask( urlDrawable);

asyncTask.execute(source);

// return reference to URLDrawable where I will change with actual image from
// the src tag
return urlDrawable;
}

public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
URLDrawable urlDrawable;

public ImageGetterAsyncTask(URLDrawable d) {
this.urlDrawable = d;
}

@Override
protected Drawable doInBackground(String... params) {
String source = params[0];
return fetchDrawable(source);
}

@Override
protected void onPostExecute(Drawable result) {
// set the correct bound according to the result from HTTP call
urlDrawable.setBounds(0, 0, 0 + result.getIntrinsicWidth(), 0
+ result.getIntrinsicHeight());

// change the reference of the current drawable to the result
// from the HTTP call
urlDrawable.drawable = result;

// redraw the image by invalidating the container
URLImageParser.this.container.invalidate();
}

/***
* Get the Drawable from URL
* @param urlString
* @return
*/
public Drawable fetchDrawable(String urlString) {
try {
InputStream is = fetch(urlString);
Drawable drawable = Drawable.createFromStream(is, "src");
drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0
+ drawable.getIntrinsicHeight());
return drawable;
} catch (Exception e) {
return null;
}
}

private InputStream fetch(String urlString) throws MalformedURLException, IOException {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(urlString);
HttpResponse response = httpClient.execute(request);
return response.getEntity().getContent();
}
}
}

Finally, below is the sample program to demonstrate how things work:

String html = "Hello " +
"<img src='http://www.gravatar.com/avatar/" +
"f9dd8b16d54f483f22c0b7a7e3d840f9?s=32&d=identicon&r=PG'/>" +
" This is a test " +
"<img src='http://www.gravatar.com/avatar/a9317e7f0a78bb10a980cadd9dd035c9?s=32&d=identicon&r=PG'/>";

this.textView = (TextView)this.findViewById(R.id.textview);
URLImageParser p = new URLImageParser(textView, this);
Spanned htmlSpan = Html.fromHtml(html, p, null);
textView.setText(htmlSpan);

How to listen for clicks on drawn images in Html.ImageGetter class

The images themselves are not clickable, but you can wrap them in custom URLSpans and listen for clicks from those URLSpans and then run your action.

Parse the HTML using Html.fromHtml(...) and do:

Spannable html = (Spannable) Html.fromHtml(...);
for (ImageSpan span : html.getSpans(0, html.length(), ImageSpan.class)) {
int flags = html.getSpanFlags(span);
int start = html.getSpanStart(span);
int end = html.getSpanEnd(span);

html.setSpan(new URLSpan(span.getSource()) {
@Override public void onClick(View v) {
/* handle click here */
}
}, start, end, flags);
}
textView.setText(html);
textView.setMovementMethod(LinkMovementMethod.getInstance());

How to get HTML ImageGetter to work properly

The way I solved this was to change

<img src =\"file:///android_asset/img/situp1.png\"> 

to

<img src=\"situp1\"> 

as it was already in the resource folder and hence a drawable.

I also had to change

ExerciseDetailFragment.this.getResources().getIdentifier(source, "drawable", "com.package.ChaitanyaVarier.mytrainer2go"); 

to

ExerciseDetailFragment.this.getResources().getIdentifier(source, "drawable", "com.ChaitanyaVarier.mytrainer2go");

A rather silly problem I had, just had to understand the code a little better.

Display images on Android using TextView and Html.ImageGetter asynchronously?

Now I'm using an AsyncTask to download the images in the ImageGetter:

Spanned spannedContent = Html.fromHtml(htmlString, new ImageGetter() {

@Override
public Drawable getDrawable(String source) {
new ImageDownloadAsyncTask().execute(textView, htmlString, source);
return null;
}
}, null);

And set the text again into the TextView when the image has been downloaded.

Now it works. But It failed when I tried to do the TextView.postInvalidate() to redraw the downloaded images. I have to do setText() again in the AsyncTask.

Does anyone know why?

Asynchronous Html.ImageGetter for setting multiple images in a TextView

Doing this synchronously is trivial, but is there a suggested way to do this asynchronously so that it doesn't lock up my UI thread?

That will be difficult, perhaps impossible. You have to return something immediately. Even if that is a placeholder, you then have the challenge of how to replace the placeholder once the download is complete. Since you lack access to the ImageView rendering the image (if there is an ImageView), I don't know how you would arrange to replace the placeholder.

Cannot display images in Html.fromHtml() in text view for images coming dynamically

I also faced a similar problem month ago and used this and it works fine :

String htmlData = listData.get(position).getValue();
String showData = htmlData.replace("\n", "");

URLImageParser p = new URLImageParser(holder.textt, context);
Spanned htmlAsSpanned = Html.fromHtml(showData,p,null);

holder.yourTextView.setText(htmlAsSpanned);

Now copy and paste these 2 methods :

First method :

public class URLDrawable extends BitmapDrawable {
protected Drawable drawable;
@Override
public void draw(Canvas canvas) {
if(drawable != null) {
drawable.draw(canvas);
}
}
}

///Second Method :

public class URLImageParser implements Html.ImageGetter {
Context c;
TextView container;

/***
* Construct the URLImageParser which will execute AsyncTask and refresh the container
* @param t
* @param c
*/
public URLImageParser(TextView t, Context c) {
this.c = c;

this.container = t;
}

public Drawable getDrawable(String source) {
URLDrawable urlDrawable = new URLDrawable();

// get the actual source
ImageGetterAsyncTask asyncTask =
new ImageGetterAsyncTask( urlDrawable);

asyncTask.execute(source);

// return reference to URLDrawable where I will change with actual image from
// the src tag
return urlDrawable;
}

public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
URLDrawable urlDrawable;

public ImageGetterAsyncTask(URLDrawable d) {
this.urlDrawable = d;
}

@Override
protected Drawable doInBackground(String... params) {
String source = params[0];
return fetchDrawable(source);
}

@Override
protected void onPostExecute(Drawable result) {
// set the correct bound according to the result from HTTP call
urlDrawable.setBounds(0, 0, 0 + result.getIntrinsicWidth(), 0
+ result.getIntrinsicHeight());

// change the reference of the current drawable to the result
// from the HTTP call
urlDrawable.drawable = result;

// redraw the image by invalidating the container
URLImageParser.this.container.invalidate();
URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight()
+ result.getIntrinsicHeight()));

}

/***
* Get the Drawable from URL
* @param urlString
* @return
*/
public Drawable fetchDrawable(String urlString) {
try {
InputStream is = fetch(urlString);
Drawable drawable = Drawable.createFromStream(is, "src");
drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0
+ drawable.getIntrinsicHeight());
return drawable;
} `enter code here`catch (Exception e) {
return null;
}
}

private InputStream fetch(String urlString) throws MalformedURLException, IOException {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(urlString);
HttpResponse response = httpClient.execute(request);
return response.getEntity().getContent();
}
}
}


Related Topics



Leave a reply



Submit