简体   繁体   中英

Images in android textview using fromHtml method doesn't show up

I'm trying to display images in my help file using the Html.fromHtml method.

This is my java code

            TextView tv = (TextView)findViewById(R.id.text);
        tv.setText(Html.fromHtml(getString(R.string.help), new Html.ImageGetter() {
            @Override
             public Drawable getDrawable(String source) {
                    int id;

                    if (source.equals("top_scrollers.png")) {
                        id = R.drawable.top_scrollers;
                    }
                    else if (source.equals("prices.png")) {
                        id = R.drawable.prices;
                    }
                    else if (source.equals("percentage.png")) {
                        id = R.drawable.percentage;
                    }
                    else if (source.equals("mplus.png")) {
                        id = R.drawable.mplus;
                    }
                    else {
                        return null;
                    }

                    Drawable d = getResources().getDrawable(id);
                    d.setBounds(0,0,d.getIntrinsicWidth(),d.getIntrinsicHeight());
                    return d;
                }
        }, null));

And this is my xml

<string name="help">
<![CDATA[
<h1>Help</h1>
<p>
<h3>Getting stone price</h3>
<img src="top_scrollers.png"/>
</p>
]]>
</string>

As you can see i'm placing the images in the cdata text, and I have created a method to create drawables from them, but it is just isn't displayable. Any suggestions?

You can use WebView instead TextView for it. Try this solution:

1) Put your top_scrollers.png in assets folder

2) Create your_html.html in raw folder with content:

<h1>Help</h1>
<p>
<h3>Getting stone price</h3>
<img src="file:///android_asset/top_scrollers.png"/>
</p> 

3) For read this raw file as string you can read this Android read text raw resource file

4) After that you can load this text in webView:

webView.loadDataWithBaseURL("", your_raw_string, "text/html", "utf-8", "");

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