how to show images inside html text using textview
Guys I have a textview that show a html text, and it has image tag inside. I want to show the image using imageGetter but it just show a little blue box. I've been working with this problem for days. so please help me with this.
my current code is this
TextView textD = (TextView) findViewById(R.id.body);
textD.setText(Html.fromHtml(Body, imgGetter, null));
textD.setMovementMethod(new ScrollingMovementMethod());
private ImageGetter imgGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
try{
InputStream is = (InputStream) new URL(source).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e){
Log.d("image", imageSource, e);
开发者_开发百科 return null;
}
}
};
The reference says:
The setBounds(Rect) method must be called to tell the Drawable where it is drawn and how large it should be. All Drawables should respect the requested size, often simply by scaling their imagery. A client can find the preferred size for some Drawables with the getIntrinsicHeight() and getIntrinsicWidth() methods.
http://developer.android.com/reference/android/graphics/drawable/Drawable.html
So try this:
d.setBounds(0,0,d.getIntrinsicWidth(),d.getIntrinsicHeight());
Itt will work!
LinearLayout ll = new LinearLayout(this);
TextView question = new TextView(this);
question.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
question.setTextSize(20);
question.setTextColor(Color.WHITE);
CharSequence text2 = Html.fromHtml(qstnText, new Html.ImageGetter(){
public Drawable getDrawable(String source){
System.out.println("Source:::"+source);
String path = Environment.getExternalStorageDirectory().getAbsolutePath() +source;
File f = new File(path);
// Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
Drawable bmp = Drawable.createFromPath(f.getAbsolutePath());
bmp.setBounds(0,0,50,50); return bmp;
}
}, null);
question.setText(text2);
ll.addView(question);
Its works fine for me... Try it
精彩评论