Creating AsyncTask in Android

Creating asynctask in android

Explains how to create an asynctask which allows the image file loading to be done in a background thread.

Part 2 – Addresses the issue with the bitmaps being loaded from the filesystem in the UI thread by creating asynctask in android which in effect creates a background thread to do the file loading.


Create the AsyncTask Class

public class BitmapWorkerTask extends AsyncTask<File, Void, Bitmap> {

    WeakReference<ImageView> imageViewReferences;

    public BitmapWorkerTask(ImageView imageView) {
        imageViewReferences = new WeakReference<ImageView>(imageView);

    protected Bitmap doInBackground(File... params) {
        return BitmapFactory.decodeFile(params[0].getAbsolutePath());

    protected void onPostExecute(Bitmap bitmap) {
        if(bitmap != null && imageViewReferences != null) {
            ImageView viewImage = imageViewReferences.get();
            if(viewImage != null) {

Call the AsyncTask

BitmapWorkerTask workerTask = new BitmapWorkerTask(holder.getImageView());

Run the App

About The Author

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>