我有一个字符串,如下所示:
String text = "<img src="https://www.google.com/images/srpr/logo3w.png">"
当我这样做时
TextView.setText(Html.fromHtml(text));
我需要将图像显示在活动中。
我不想使用WebView。
请告知我们实现这一目标的其他方法。
答案 0 :(得分:1)
你必须使用asynctask,在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);
}
希望它会有所帮助。
答案 1 :(得分:1)
我在以下链接中遵循了这个想法。
http://www.docstoc.com/docs/120417139/How-To-Implement-Htmlfromhtml-With-Imagegetter-In-Android
http://shiamiprogrammingnotes.blogspot.com/2010/09/textview-with-html-content-with-images.html
答案 2 :(得分: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)));