How to set image from Url using AsyncTask?

后端 未结 7 1231
青春惊慌失措
青春惊慌失措 2021-02-06 14:18

I\'m a newbie programmer an I\'m making an android program that displays an image on ImageView from a given url. My problem is how do you use this on the AsyncTask?

Thes

7条回答
  •  日久生厌
    2021-02-06 14:40

    Try this code, make your drawable variable global and change your satellite function like this:

    private void satellite() {
          // TODO Auto-generated method stub
          ImageView imgView =(ImageView)findViewById(R.id.satellite);
          new yourTask().execute();
    }
    

    then create asyncTask class like this:

    private class yourTask extends AsyncTask {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            //show a progress bar
        }
    
        @Override
        protected String doInBackground(Integer... params) {
            drawable  =  LoadImageFromWeb("http://www.pagasa.dost.gov.ph/wb/sat_images/satellite.gif"); 
            return 0; 
        }      
    
        @Override
        protected void onPostExecute(Integer result) {
            super.onPostExecute(result);
            imgView.setImageDrawable(drawable);   
        }
    }
    

提交回复
热议问题