How to set image from Url using AsyncTask?

后端 未结 7 1230
青春惊慌失措
青春惊慌失措 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:42

    Here is the code for the Aynctask implementation Make your drawable object as global in Aynctask class.

    Class MyDownloader extends AsyncTask{
    
        Drawable drawable;    
        @Override
        public String doInBackground(Void... args){
    
            drawable = LoadImageFromWeb("http://www.pagasa.dost.gov.ph/wb/sat_images/satellite.gif");
            return null; // here you can pass any string on response as on error or on success
    
        }
    
        public void onPostExecute(String result){
    
            if(drawable!=null){
    
                imgView.setImageDrawable(drawable);
    
            }
    
        }
    
    }
    

    now create object of this class and execute it

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

    Here is good example link for caching the image check out this link and example

    https://github.com/novoda/ImageLoader

提交回复
热议问题