The below solution is what worked for me. I found that doing a distinct in the projections was not an option for the Media content provider. Also, this seems to work with a cursorLoader.
// TODO Auto-generated method stub
String[] projection = { Images.Media._ID, Images.Media.BUCKET_DISPLAY_NAME };
HashMap<String, String> imagesGroups = new HashMap<String, String>();
String ids = null;
Cursor c = _myContext.getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
if (c.getCount() > 0)
{
c.moveToFirst();
do
{
String bucketDisplayName = c.getString(c.getColumnIndex(Images.Media.BUCKET_DISPLAY_NAME));
String _id = c.getString(c.getColumnIndex(Images.Media._ID));
//here is where we ensure we get a unique image id for each distinct bucket display name
if(!imagesGroups.containsKey(bucketDisplayName))
{
imagesGroups.put(bucketDisplayName, _id);
if(ids == null)
ids = _id;
else
ids += "," + _id;
}
}
while (c.moveToNext());
}
c.close();
String selection = Images.Media._ID + " IN (" + ids + ")";
CursorLoader cursorLoader = new CursorLoader(this, Images.Media.EXTERNAL_CONTENT_URI, projection, selection, null, null);
return cursorLoader;