图像未使用Html.fromhtml在textview中显示

时间:2013-01-17 12:33:28

标签: android image textview

我的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个默认小方块,绿色而不是图像

请帮我显示图片

提前致谢

2 个答案:

答案 0 :(得分:5)

试试这个:

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);

它适用于我。

感谢。

答案 1 :(得分:3)

它对我有用,如下

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));