2015-11-20 27 views
0

我需要在TextView中插入一个我从代码中从网上下载的drawable。所以我不能手动将图像放在res和R.drawable中。Android从java代码添加可绘制图像?

我该如何使用它?

+0

从网上下载位图/ jpeg后,您可以更改为可绘制或您可以按原样使用。 – Ajitha

+0

看看http://stackoverflow.com/questions/12652798/loading-drawable-from-sd-card回答你的问题。 – jmrodrigg

+1

如何在TextView中插入drawable? –

回答

2

步骤:

1)下载图像

2)转换通过调用水木清华这样来绘制

3)设置绘制到的TextView:

textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0); 
0

试试这个

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

参考网址How to display an image from an URL within textView