silversunhunter
silversunhunter

Reputation: 1269

Getting a BitMap returned from AsyncTask in Java

I am having trouble understanding AsyncTask. I am sending a file location to it so that it can retrieve it, resize it and return it. When I call the asynctask class from my activity, which is a BaseAdapter ListView I am getting a type mismatch.

I thought that I could return the Bitmap from the execute. I have read that I need to use the onPostExecute() but am unsure how..

public class ImageHandler extends AsyncTask<String, Void, Bitmap> {
        Bitmap sizedBMP = null;
        @Override

        protected Bitmap doInBackground(String... params) {

            File imgFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath(), "/"+params[0]+"/"+params[0]+".png");
            if (imgFile.exists()){
                Bitmap bmp = BitmapFactory.decodeFile(imgFile.toString());
                int newWidth = 500;
                sizedBMP = getResizedBitmap(bmp, newWidth);

            }
            else{
                //set no image available
            }
            return sizedBMP;

        }

        @Override
        // Once the image is downloaded, associates it to the imageView
        protected void onPostExecute(Bitmap sizedBMP) {

        }


    public Bitmap getResizedBitmap(Bitmap bm, int newWidth) {
        // Bitmap gets resized here.....

        }
}

I am calling this class inside the getView() like:

ImageHandler imgHandler = new ImageHandler();
Bitmap bitMap;
bitMap = imgHandler.execute(filename);

Error:

Type mismatch: cannot convert from AsyncTask to Bitmap

Upvotes: 0

Views: 339

Answers (1)

Borislav Kamenov
Borislav Kamenov

Reputation: 711

The method .execute returns the current AsyncTask. To set the result, add a constructor with parameter the imageview you want to use to show the bitmap.

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

        Bitmap sizedBMP = null;
        ImageView imageView;

        public ImageHandler(ImageView imageView){
             this.imageView = imageView;
        }

        @Override
        protected Bitmap doInBackground(String... params) {

            File imgFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath(), "/"+params[0]+"/"+params[0]+".png");
            if (imgFile.exists()){
                Bitmap bmp = BitmapFactory.decodeFile(imgFile.toString());
                int newWidth = 500;
                sizedBMP = getResizedBitmap(bmp, newWidth);

            }
            else{
                //set no image available
            }
            return sizedBMP;

        }

        @Override
        // Once the image is downloaded, associates it to the imageView
        protected void onPostExecute(Bitmap sizedBMP) {
                if(sizedBMP != null){
                    imageView.setImageBitmap(sizedBMP);
                }
        }


    public Bitmap getResizedBitmap(Bitmap bm, int newWidth) {
        // Bitmap gets resized here.....

        }
}

and then set the imageView in the constructor like this way:

ImageHandler imgHandler = new ImageHandler(myImageView);
imgHandler.execute(filename);

That should works but if you want to use this type of image loader, when scroll the listview you may have other problem - for a single ImageView slot in a row few bitmaps will replace each other for few seconds.

This topic can help you with that.

Upvotes: 1

Related Questions