简体   繁体   中英

Image is not displaying in textview using Html.fromhtml

I have the HTML string as follows

String htmlstr=" This is my image <img src='/sdcard/pic1.jpg' /> and the my second image is <img src='/sdcard/pic2.jpg' />"

I am using

txtimg.setText(Html.fromHtml(htmlstr));

But the problem is it displays 1 default small squre with green color instead of image

Please help me to display a image

Thanks in advance

Try this :

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

It works well for me.

Thanks.

It worked for me as follows

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM