Neelay Srivastava
Neelay Srivastava

Reputation: 1241

How to get the distinct element from mediastore by java

I am trying to get the file names of all the audio files but I am getting same file names for multiple songs

1.I cannot use DISTINCT key word as I am getting the file names from DATA .

2.I am using the Mediastore.Files So the select it takes MEDIA_TYPE so this way is also not possible .

3 .I want to get the Parent value as distinct not the repeating value .

So the only way is by doing in java .I followed the method given here but I am not able to set

Here is a piece of my code

if (audioCursor.moveToFirst()) {
    do {
        int filetitle = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.TITLE);
        int file_id = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID);
        int fileparent = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.PARENT);
        int filedata = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA);
        Mediafileinfo info = new Mediafileinfo();

        info.setData(new File(new File(audioCursor.getString(filedata)).getParent()).getName());
        info.setTitle(audioCursor.getString(filetitle));
        info.set_id(audioCursor.getString(file_id));
        info.setParent(audioCursor.getString(fileparent));

        audioList.add(info);
    } while (audioCursor.moveToNext());
}

How I can get the non repeating elements?? For more info mediastore.file I am adding the data in Mediafileinfo class which contain getter and setter.

Upvotes: 0

Views: 335

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191864

I want to get the Parent value as distinct not the repeating value .

Alright, you could use a HashSet<String> to maintain a list of seen MediaStore.Files.FileColumns.PARENT values.

Not sure what was wrong with the SQL approach, though.

HashSet<String> seenParents = new HashSet<String>();

if (audioCursor.moveToFirst()) {
    final int fileparent = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.PARENT);
    do {
        String parent = audioCursor.getString(fileparent);

        Mediafileinfo info = new Mediafileinfo();
        // bla...
        info.setParent(parent);

        if (!seenParents.contains(parent)) { // prevents dups
            seenParents.add(parent);
            audioList.add(info);
        }

// end loop

Upvotes: 1

Related Questions