Search code examples
androiduser-interfaceandroid-studioandroid-asynctask

UI Freezes when Executing AsyncTask


I'm downloading an image from sever using AsyncTask but when calling downloadImage() from activity onCreate() the activity freezes til AsyncTask finishes downloading the image.

I'm using this code:

  Bitmap downloadImage() {
        ImageDownloader task = new ImageDownloader();
        Bitmap myImage;

        try {
            myImage = task.execute("https://drive.google.com/uc?id=0B51TujFYa0RBUUYzSE15WHZRR3c").get();

            downloadedImg.setImageBitmap(myImage);

        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.i("Interaction", "Button Tapped");

        return myImage
   }



    public class ImageDownloader extends AsyncTask<String, Void, Bitmap> {

        @Override
        protected Bitmap doInBackground(String... urls) {
            try {
                URL url = new URL(urls[0]);
                HttpURLConnection connection = (HttpsURLConnection) url.openConnection();
                connection.connect();

                InputStream inputStream = connection.getInputStream();
                //to convert Image from string data to bitmap
                Bitmap myBitmap = BitmapFactory.decodeStream(inputStream);

                return myBitmap;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

Solution

  • You don't properly use the AysncTask class. By calling get(), you pause your main thread until the image is downloaded. That's why the UI feels slow.

    Instead, you should set the image from the task's onPostExecute method:

    private Toolbar toolbar;
    private ImageView downloadedImg;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        // ... omitted ...
    
        ImageDownloader task = new ImageDownloader();    
        task.execute("https://drive.google.com/uc?id=0B51TujFYa0RBUUYzSE15WHZRR3c");
    
    }
    
    // ... omitted ...    
    
    public class ImageDownloader extends AsyncTask<String, Void, Bitmap> {
    
        @Override
        protected Bitmap doInBackground(String... urls) {
            try {
                URL url = new URL(urls[0]);
                HttpURLConnection connection = (HttpsURLConnection) url.openConnection();
                connection.connect();
    
                InputStream inputStream = connection.getInputStream();
                //to convert Image from string data to bitmap
                Bitmap myBitmap = BitmapFactory.decodeStream(inputStream);
    
                return myBitmap;
    
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        protected void onPostExecute(Bitmap result) {
    
            downloadedImg.setImageBitmap(result);
        }
    }