2015-11-20 39 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