Neelay Srivastava Neelay Srivastava - 11 months ago 54
Java Question

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

key word as I am getting the file names from

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

3 .I want to get the
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 != null) {
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());

} 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.

Answer Source

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...

        if (!seenParents.contains(parent)) { // prevents dups

// end loop