Android 使用TextView.setText(html.fromHtml(string))将具有html图像标记的字符串转换为图像
我有一个字符串,如下所示:Android 使用TextView.setText(html.fromHtml(string))将具有html图像标记的字符串转换为图像,android,android-layout,Android,Android Layout,我有一个字符串,如下所示: String text = "<img src="https://www.google.com/images/srpr/logo3w.png">" 我需要在活动中显示图像 我不想使用网络视图。 请告诉我实现此目的的其他方法。您必须使用asynctask,在doInbackground()中打开连接。在onPostExecute()中将图像设置为文本视图。 希望能有所帮助。您需要在strings.xml <string name="nice_html
String text = "<img src="https://www.google.com/images/srpr/logo3w.png">"
我需要在活动中显示图像
我不想使用网络视图。
请告诉我实现此目的的其他方法。您必须使用asynctask,在
doInbackground()中打开连接。
在onPostExecute()中将图像设置为文本视图。
希望能有所帮助。您需要在
strings.xml
<string name="nice_html">
<![CDATA[<img src='https://www.google.com/images/srpr/logo3w.png'>
]]></string>
我在下面的链接中遵循了这个想法
您想在
文本视图中设置URL,或者提取URL,获取图像并在图像视图中显示@Santhosh V M使用时有什么问题WebView@SiddharthLele我不想使用imageView。有时我传递的字符串中不会有html图像标记。@SantoshVM:标记也会改变吗?@SantoshVM尝试我的答案。
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);
}
<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)));