Santosh  V M
Santosh V M

Reputation: 1561

convert string having html image tag into image using TextView.setText(Html.fromHtml(string))

I have a string which as follows:

String text = "<img src="https://www.google.com/images/srpr/logo3w.png">"

When I do

TextView.setText(Html.fromHtml(text));

I need the image to be displayed in the activity.

I do not want to use a WebView.

Please let me know as to what are the other ways to achieve this.

Upvotes: 2

Views: 1657

Answers (3)

Nezam
Nezam

Reputation: 4132

You need to define your text in strings.xml

<string name="nice_html">
<![CDATA[<img src='https://www.google.com/images/srpr/logo3w.png'>
]]></string>

Then, in your code:

TextView foo = (TextView)findViewById(R.id.foo); 
foo.setText(Html.fromHtml(getString(R.string.nice_html)));

Upvotes: 0

KDeogharkar
KDeogharkar

Reputation: 10959

you have to use asynctask,open connection in doInbackground() set image to textview in onPostExecute()

try {
        /* Open a new URL and get the InputStream to load data from it. */
        URL aURL = new URL("ur Image URL");
        URLConnection conn = aURL.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        /* Buffered is always good for a performance plus. */
        BufferedInputStream bis = new BufferedInputStream(is);
        /* Decode url-data to a bitmap. */
        Bitmap bm = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();

        Drawable d =new BitmapDrawable(bm);
       d.setId("1");
 textview.setCompoundDrawablesWithIntrinsicBounds(0,0,1,0);// wherever u want the image relative to textview
        } catch (IOException e) {
        Log.e("error", "Remote Image Exception", e);
        } 

Hope it will help.

Upvotes: 1

Related Questions