在TextView中显示<img src=""/> html标签内的图片,大家都知道,在TextView中显示HTML内容的方法如下所示:
如果HTML中有图片的话,显示出来的图片会被一个小框取代,那么怎么样才能看到图片呢?查看了一下API,android.text.Html还还有另一个方法:Html.fromHtml(String source,ImageGetter imageGetter,TagHandler tagHandler),这个方法使用如下所示:
会报空指针错误,关于这点,我是这么理解的,drawable = Drawable.createFromPath(source);这个方法中的path(即source)是文件系统路径,即本地图片的 比如:
TextView description=(TextView)findViewById(R.id.description);
description.setText(Html.fromHtml(item.getDescription()));
如果HTML中有图片的话,显示出来的图片会被一个小框取代,那么怎么样才能看到图片呢?查看了一下API,android.text.Html还还有另一个方法:Html.fromHtml(String source,ImageGetter imageGetter,TagHandler tagHandler),这个方法使用如下所示:
ImageGetter imgGetter = new Html.ImageGetter() { public Drawable getDrawable(String source) { Drawable drawable = null; Log.d("Image Path", source); URL url; try { url = new URL(source); drawable = Drawable.createFromStream(url.openStream(), ""); } catch (Exception e) { return null; } drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable .getIntrinsicHeight()); return drawable; } }; ......... TextView description=(TextView)findViewById(R.id.description); description.setText(Html.fromHtml(item.getDescription(),imgGetter,null));
当然也可以是本地图片的显示,只要在getDrawable(String source)里进行设置即可,这点比较简单,还有个小问题就是当我们使用如下方法处理TextView显示<img src=..>:
ImageGetter imgGetter = new Html.ImageGetter() {
public Drawable getDrawable(String source) {
Drawable drawable = null;
Log.d("Image Path", source);
try {
drawable = Drawable.createFromPath(source);
} catch (Exception e) {
return null;
}
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable
.getIntrinsicHeight());
return drawable;
}
};
.........
TextView description=(TextView)findViewById(R.id.description);
description.setText(Html.fromHtml(item.getDescription(),imgGetter,null));
会报空指针错误,关于这点,我是这么理解的,drawable = Drawable.createFromPath(source);这个方法中的path(即source)是文件系统路径,即本地图片的 比如:
d = Drawable.createFromPath(getFilesDir() + "/test.9.png");
多多指教