How to set image from Url using AsyncTask?

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

    If the image is not that big you can just use an anonymous class for the async task. This would like this:

    ImageView mChart = (ImageView) findViewById(R.id.imageview);
    String URL = "http://www...anything ...";
    
    mChart.setTag(URL);
    new DownloadImageTask.execute(mChart);
    

    and the task class is

    public class DownloadImagesTask extends AsyncTask {
       ImageView imageView = null;
       @Override
       protected Bitmap doInBackground(ImageView... imageViews) {
          this.imageView = imageViews[0];
          return download_Image((String)imageView.getTag());
       }
    
       @Override
       protected void onPostExecute(Bitmap result) {
          imageView.setImageBitmap(result);
       }
    
       private Bitmap download_Image(String url) {
           ...
       }
    

提交回复
热议问题