Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7616537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:54:45+00:00 2026-05-31T02:54:45+00:00

I have been trying to query for all images on sd card through MediaStore

  • 0

I have been trying to query for all images on sd card through MediaStore content provider and display their thumbnail on a GridView.

However, if i load the image thumbnail on the main thread, the scrolling gets incredibly slow…

So i tried to load the bitmap through asynctasks:
Scrolling performance got better, but now the grid items keep reloading their thumbnail until it gets the correct bitmap…

Here is my asynctask, which loads the bitmaps:

package x.y;

import java.lang.ref.WeakReference;

import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory.Options;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.widget.ImageView;

public class ImageThumbnailLoader extends AsyncTask<Long, Void, Bitmap> {

    private final Options mOptions;

    private WeakReference<ImageView> mImageViewWeakReference;
    private ContentResolver mContentResolver;

        public ImageThumbnailLoader(ImageView imageView,
                ContentResolver cr) {
        mContentResolver = cr;
        mImageViewWeakReference = new WeakReference<ImageView>(imageView);
        mOptions = new Options();
        mOptions.inSampleSize = 4;
    }



    @Override
    protected Bitmap doInBackground(Long... params) {
        Bitmap result;
            result = MediaStore.Images.Thumbnails.getThumbnail(
                    mContentResolver, params[0],
                    MediaStore.Images.Thumbnails.MINI_KIND, mOptions);
        return result;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);
            if (mImageViewWeakReference != null
                    && mImageViewWeakReference.get() != null)
                mImageViewWeakReference.get().setImageBitmap(result);
    }

}

And here is my custom cursor adapter:

package x.y;

import android.content.Context;
import android.database.Cursor;
import android.graphics.BitmapFactory.Options;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;

public class MediaCursorAdapter extends CursorAdapter {

    private LayoutInflater mInflater;
    private final static int mColumnID = 0;
    private Options mOptions;

    public MediaCursorAdapter(Context context, Cursor c) {
        super(context, c);

        mInflater = LayoutInflater.from(context);
        mOptions = new Options();
        mOptions.inSampleSize = 4;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        ViewHolder holder = (ViewHolder) view.getTag();
        ImageThumbnailLoader imageLoader = new ImageThumbnailLoader(holder.thumbImg,
                context.getContentResolver());
        imageLoader.execute(cursor.getLong(mColumnID));
//      holder.thumbImg.setImageBitmap(MediaStore.Images.Thumbnails.getThumbnail(
//                  context.getContentResolver(), cursor.getLong(mColumnID),
//                  MediaStore.Images.Thumbnails.MINI_KIND, mOptions));
        Log.i("Prototype", "bindView : " + cursor.getPosition());
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        Log.i("Prototype", "newView : " + cursor.getPosition());
        View view = mInflater.inflate(R.layout.grid_item, null);
        ViewHolder holder = new ViewHolder(view);
        view.setTag(holder);
        return view;
    }


    private static class ViewHolder {
        ImageView thumbImg, dragImg;

        ViewHolder(View base) {
            thumbImg = (ImageView) base.findViewById(R.id.thumbImage);
            dragImg = (ImageView) base.findViewById(R.id.dragImage);
        }
    }

}

I query the cursor with this code and send it to the adapter:

query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Images.Media._ID,
            MediaStore.Images.Media.DATA }, null, null,
    MediaStore.Images.Media._ID);

Looks like the bindview() on my custom cursor adapter gets called more often than it is supposed to… Anyone knows how can i make the images on my gridview stop reloading while mantaining the scrolling performance??

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:54:47+00:00Added an answer on May 31, 2026 at 2:54 am

    Problem solved, had to check if image at the start of async task was the same as the image at the end of it, on onPostExecute().

    new bindView:

    @Override
        public void bindView(View view, Context context, Cursor cursor) {
    
            ViewHolder holder = (ViewHolder) view.getTag();
            holder.thumbImg.setId(cursor.getPosition());
            ImageThumbnailLoader imageLoader = new ImageThumbnailLoader(holder.thumbImg,
                context.getContentResolver());
            imageLoader.execute(cursor.getLong(mColumnID));
        Log.i("Prototype", "bindView : " + cursor.getPosition());
        }
    

    new Async:

    public class ImageThumbnailLoader extends AsyncTask<Long, Void, Bitmap> {
    
        private final Options mOptions;
    
        private WeakReference<ImageView> mImageViewWeakReference;
        private ContentResolver mContentResolver;
        private int mPosition;
    
    
            public ImageThumbnailLoader(ImageView imageView,
                ContentResolver cr) {
            mContentResolver = cr;
            mImageViewWeakReference = new WeakReference<ImageView>(imageView);
            mOptions = new Options();
            mOptions.inSampleSize = 4;
            mPosition = imageView.getId();
        }
    
    
    
        @Override
        protected Bitmap doInBackground(Long... params) {
            Bitmap result;
                result = MediaStore.Images.Thumbnails.getThumbnail(
                    mContentResolver, params[0],
                    MediaStore.Images.Thumbnails.MINI_KIND, mOptions);
            return result;
        }
    
        @Override
        protected void onPostExecute(Bitmap result) {
            super.onPostExecute(result);
                if (mImageViewWeakReference != null
                        && mImageViewWeakReference.get() != null
                            && mPosition == mImageViewWeakReference.get().getId())
                    mImageViewWeakReference.get().setImageBitmap(result);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to build an SQL query to return matches between three
in the past hour i have been trying different variants of this query but
I have been trying to introduce a Merge statement into the following SQL query
I have been trying to track down a problem with a query I have.
I have the following mysql table. I have been trying to select all the
I have been trying out some LINQ query can someone please show how to
I have been trying to get this query to work for the longest and
I have been trying to figure out this query for a few hours now
All, I have been trying to work out how to select say 15 tickets
I have been trying to display a table of results for weekly sales using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.