0

I need to insert in a TextView a drawable that I download from web in the code. So I can't manually put the image in res and in R.drawable.

How can I use it?

Andrean
  • 313
  • 1
  • 4
  • 13

2 Answers2

2

Steps:

1) Download image

2) Convert to drawable

3) Set drawable to textView by calling smth like this:

textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);
winston
  • 875
  • 1
  • 8
  • 15
0

Try this

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

Reference url How to display an image from an URL within textView

Community
  • 1
  • 1
Shubhank Gupta
  • 833
  • 2
  • 15
  • 35