Does Android AsyncTaskQueue or similar exist?

Posted by Ben L. on Stack Overflow See other posts from Stack Overflow or by Ben L.
Published on 2010-06-12T18:28:58Z Indexed on 2010/06/12 18:32 UTC
Read the original article Hit count: 304

Filed under:
|

I read somewhere (and have observed) that starting threads is slow. I always assumed that AsyncTask created and reused a single thread because it required being started inside the UI thread.

The following (anonymized) code is called from a ListAdapter's getView method to load images asynchronously. It works well until the user moves the list quickly, and then it becomes "janky".

final File imageFile = new File(getCacheDir().getPath() + "/img/" + p.image);
image.setVisibility(View.GONE);
view.findViewById(R.id.imageLoading).setVisibility(View.VISIBLE);
(new AsyncTask<Void, Void, Bitmap>() {
    @Override
    protected Bitmap doInBackground(Void... params) {
        try {
            Bitmap image;
            if (!imageFile.exists() || imageFile.length() == 0) {
                image = BitmapFactory.decodeStream(new URL(
                        "http://example.com/images/"
                                + p.image).openStream());
                image.compress(Bitmap.CompressFormat.JPEG, 85,
                        new FileOutputStream(imageFile));
                image.recycle();
            }
            image = BitmapFactory.decodeFile(imageFile.getPath(),
                bitmapOptions);
            return image;
        } catch (MalformedURLException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
            return null;
        } catch (IOException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    protected void onPostExecute(Bitmap image) {
        if (view.getTag() != p) // The view was recycled.
            return;
            view.findViewById(R.id.imageLoading).setVisibility(
                View.GONE);
        view.findViewById(R.id.image)
                .setVisibility(View.VISIBLE);
        ((ImageView) view.findViewById(R.id.image))
                .setImageBitmap(image);
    }
}).execute();

I'm thinking that a queue-based method would work better, but I'm wondering if there is one or if I should attempt to create my own implementation.

© Stack Overflow or respective owner

Related posts about android

Related posts about android-sdk