hardik9850
hardik9850

Reputation: 1036

Android duplicate thumbnail issue

I am creating a custom image and video gallery just like Whatsapp have to allow user to select multiple image/video,so far i am populating and showing thumbnails on gridview.But the result is,on some devices the images sometimes got shown duplicated and on some they do not.same happens with video gallery also.

I am quering the ID of Images from

 MediaStore.images 

and get thumbnails using

MediaStore.images.thumbnails.getThumbnail 

from obtained IDs

below is my code

            final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
            final String orderBy = MediaStore.Images.Media.DATE_ADDED;
            imageCursor = activity.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,null, orderBy+" DESC");
            int image_column_index = imageCursor.getColumnIndex(MediaStore.Images.Media._ID);
            int count = imageCursor.getCount();
            thumbnails = new Bitmap[count];
            arrPath = new String[count];
            Log.d("count ", ""+count);
            int i=0;


            for ( i= 0; i <count; i++) 
            {
                imageCursor.moveToPosition(i);
                int id = imageCursor.getInt(image_column_index);
                int dataColumnIndex = imageCursor.getColumnIndex(MediaStore.Images.Media._ID);
                thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(), id,MediaStore.Images.Thumbnails.MICRO_KIND, null);
                arrPath[i]= imageCursor.getString(dataColumnIndex);
            }

from android documentation getThumbnail should create thumbnail if it is not available.

one example that produce same issue is MediaStore.Images.Thumbnails.getThumbnail returns wrong thumbnail instead of NULL but that guy hasn't found the solution (question was asked 2 years ago).

Has anybody faced this problem,i have gone through many examples and to add more i am loading cursor in asynctask so populating thubnails should not be a problem for Adroid OS i guess.Is there a solution available for this issue?Its quite frustrating.

Upvotes: 4

Views: 1143

Answers (1)

Kyle Venn
Kyle Venn

Reputation: 8038

I'm not sure if you've found the solution to your problem yet but I had a similar issue. My app was returning thumbnail uri's that didn't point to anything. But a call to ThumbnailUtils.createVideoThumbnail() would fix the reference in the content resolver. I ultimately dropped something like the below into my app:

Bitmap bitmap = Thumbnails.getThumbnail(mActivity.getContentResolver(), videoFile.mId,
                                        Thumbnails.MINI_KIND, null);
if (bitmap == null) {
   ThumbnailUtils.createVideoThumbnail(videoFile.mAbsolutePath, Thumbnails.MINI_KIND);
}

Then I told my adapter to update for the element that was affected.

Upvotes: 1

Related Questions