我的 HTML 字符串如下
String htmlstr=" This is my image <img src='/sdcard/pic1.jpg' /> and the my second image is <img src='/sdcard/pic2.jpg' />"
我在用
txtimg.setText(Html.fromHtml(htmlstr));
但问题是它显示 1 个默认的绿色小方块而不是图像
请帮我显示图像
提前致谢
尝试这个 :
final String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/test.jpg";
ImageGetter imageGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable d = Drawable.createFromPath(path);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
Spanned htmlstr= Html.fromHtml("<img src='" + path + "'/>", imageGetter, null);
TextView out_unit1 = (TextView) findViewById(R.id.mTxtViewCm2);
out_unit1.setText(htmlstr);
这对我来说很有用。
谢谢。
它对我有用如下
txtimg.setText(Html.fromHtml(htmlstr, new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
String path = source;
Drawable bmp = Drawable.createFromPath(path);
bmp.setBounds(0, 0, bmp.getIntrinsicWidth(), bmp.getIntrinsicHeight());
return bmp;
}
}, null));