How to get path of thumbnail from mediastore in reasonable time to load images in a gridview?

506 Views Asked by At

I'm trying to show thumbnails of images from sdcard in a gridview. I'm using universal image loader and image loading is takes reasonable time.

My problem is: trying to get all paths for the thumbnails takes too much time and causes lag on the gridview scroll. If i try ro read all paths ahead of the loading images then it takes 10 seconds (500 images on S5). I see a lot of app already solved this problem and it seems i am making some fundemantal mistakes here, yet i couldn't find what it is.

PS: I got this code from an answer on stackoverflow and i think my problem is not related to code but it is related with my approach to the problem. Here is the code:

public static String getThumbnailPath(Activity activity, long imageId) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

    CursorLoader cursorLoader = new CursorLoader(activity, uri, proj, null, null, null);
    Cursor cursor = cursorLoader.loadInBackground();



    cursor.moveToFirst();

    String result="";
    cursor = MediaStore.Images.Thumbnails.queryMiniThumbnail(activity.getContentResolver(), imageId,
            MediaStore.Images.Thumbnails.MINI_KIND, null);
    if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        result = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
        cursor.close();
    }

    result = "file://" +  result;
    return result;
}
1

There are 1 best solutions below

0
On

Have a look at smoothie by Lucas Rocha. It is a library project for fast scrolling listviews and gridviews. I use it in my app Playlist Manager. Easy to implement.