Hemantwagh07
Hemantwagh07

Reputation: 1576

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

Upvotes: 1

Views: 4809

Answers (2)

Hemantwagh07
Hemantwagh07

Reputation: 1576

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

Upvotes: 3

Pratik Sharma
Pratik Sharma

Reputation: 13415

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.

Upvotes: 5

Related Questions