如何从内部的TextView的URL显示图像 [英] How to display an image from an URL within textView

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

问题描述

我有一个TextView。在我的code我加入文本中有一些线条。我也希望显示来自外部URL一些图像(不是从我的资源文件夹)就在这些行之间。每一件事情是动态的,也就是说文本生成图像URL将在flow.So产生的,我有通过我的code,以获取图像,并将其添加。

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() 设置图像中的TextView onPostExecute()

U 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);
        } 

希望它能帮助

hope it helps

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

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