How to query Android MediaStore Content Provider, avoiding orphaned images?

Solution 1:

Okay, I've found the problem with this code sample.

In the onCreate() method, I had this line:

mImageCursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                             projection, selection, selectionArgs, null );

The problem here is that it's querying for the thumbnails, rather than the actual images. The camera app on HTC devices does not create thumbnails by default, and so this query will fail to return images that do not already have thumbnails calculated.

Instead, query for the actual images themselves:

mImageCursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                             projection, selection, selectionArgs, null );

This will return a cursor containing all the full-sized images on the system. You can then call:

Bitmap bm = MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(),
        imageId, MediaStore.Images.Thumbnails.MINI_KIND, null);

which will return the medium-sized thumbnail for the associated full-size image, generating it if necessary. To get the micro-sized thumbnail, just use MediaStore.Images.Thumbnails.MICRO_KIND instead.

This also solved the problem of finding thumbnails that have dangling references to the original full-sized images.

Solution 2:

Please note that things will be changing soon, managedQuery method is deprecated. Use CursorLoader instead(since api level 11).