如何在 textView 中显示来自 URL 的图像 [英] How to display an image from an URL within textView

查看:42
本文介绍了如何在 textView 中显示来自 URL 的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本视图.在我的代码中,我在其中添加了一些文本行.我还想在这些行之间显示来自外部 URL(不是来自我的资源文件夹)的一些图像.每件事都是动态的,即生成的文本和图像 URL 将在流中生成.所以我必须通过我的代码获取图像并添加它.

I have a textView. In my code I am adding some lines of text in it. I also want to display some image from an external URL (not from my resource folder) just in between those lines. Every thing is dynamic i.e. the text generated and the image URL will be generated on the flow.So i have to fetch the image through my code and add it.

想知道是否有办法在文本视图中插入来自外部 URL 的图像?也欢迎任何更好的方法.

Wondering if there is a way to insert images from external URL within text view? Also any better approach is always welcome.

推荐答案

您必须将它与 asynctask 一起使用,在 doInbackground() 中打开连接在 onPostExecute()

You will have to use this along with asynctask, open connection in doInbackground() set image to textview in 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);
        } 

希望能帮到你

这篇关于如何在 textView 中显示来自 URL 的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆