2

我有一个字符串,如下所示:

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

当我做

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

我需要在活动中显示图像。

我不想使用 WebView。

请让我知道实现这一目标的其他方法是什么。

4

3 回答 3

1

你必须使用异步任务,在doInbackground()设置图像到文本视图中打开连接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);
        } 

希望它会有所帮助。

于 2013-02-25T04:08:41.943 回答
0

您需要在strings.xml

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

然后,在您的代码中:

TextView foo = (TextView)findViewById(R.id.foo); 
foo.setText(Html.fromHtml(getString(R.string.nice_html)));
于 2013-02-25T05:17:30.897 回答