我有一个字符串,如下所示:
String text = "<img src="https://www.google.com/images/srpr/logo3w.png">"
当我这样做的时候
TextView.setText(Html.fromHtml(text));
我需要在 Activity 中显示图像。
我不想使用 WebView。
请告诉我还有哪些其他方法可以实现这一目标。
最佳答案
你必须使用asynctask,在doInbackground()
中打开连接,在onPostExecute()
中将图像设置为textview
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);
}
希望能有所帮助。
关于android - 使用 TextView.setText(Html.fromHtml(string)) 将具有 html 图像标签的字符串转换为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15059888/