1

I need a distinct query for Album wise image and video display, like gallery in Android. I attached my source code below. My problem is that I'm getting repeated folder names displaying. How can I use a distinct query in this code?

String[] PROJECTION_BUCKET = {"DISTINCT "+ ImageColumns.BUCKET_ID,
                ImageColumns.BUCKET_DISPLAY_NAME, ImageColumns.DATE_TAKEN,
                ImageColumns.DATA }; 
Cursor cur = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, PROJECTION_BUCKET,
                MediaStore.Images.Media.DATA + " like ? ",new String[] {"%" + "MidasOnline/Images_UserID_95"+fname+"" + "%"}, null);` 
Brian
  • 14,610
  • 7
  • 35
  • 43
shakthivel
  • 199
  • 2
  • 3
  • 14

3 Answers3

2

You need to add GROUPBY in selection for DISTINCT to work

ImageColumns.BUCKET_ID+" IS NOT NULL) GROUP BY ("+ImageColumns.BUCKET_ID

more info at this link.

Community
  • 1
  • 1
Muhammad Hamza Shahid
  • 956
  • 1
  • 15
  • 23
1

try it:

String[] projection= { "DISTINCT _id", "DISTINCT address"};

Cursor address =  getContentResolver().query(android.provider.Telephony.Sms.Inbox.CONTENT_URI,projection, null, null,"address ASC");
Elton da Costa
  • 1,279
  • 16
  • 27
-1

Take a look at this answer.

val uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
val projection = arrayOf(
    "DISTINCT " + MediaStore.Images.Media.BUCKET_ID,
    MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
    MediaStore.Images.Media.BUCKET_ID,
    MediaStore.MediaColumns.DATA
)
val groupBySelection = " 1) GROUP BY (${MediaStore.Images.Media.BUCKET_ID}"
contentResolver.query(
    uri,
    projection,
    null,
    groupBySelection,
    null,
    null
)
KLM
  • 69
  • 1
  • 4