How to display an image from an URL within textView

后端 未结 2 617
野的像风
野的像风 2020-12-20 01:54

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 thos

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-20 02:10

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

    hope it helps

提交回复
热议问题