我有一个 TextView 。在我的代码中,我在其中添加了一些文本行。我还想在这些行之间显示来自外部 URL(而不是来 self 的资源文件夹)的一些图像。每件事都是动态的,即生成的文本和图像 URL 将在流程上生成。所以我必须通过我的代码获取图像并添加它。
想知道是否有办法在 TextView 中插入来自外部 URL 的图像?任何更好的方法也总是受欢迎的。
最佳答案
您必须将其与 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("DEBUGTAG", "Remote Image Exception", e);
}
希望对你有帮助
关于android - 如何在 textView 中显示来自 URL 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8464506/