How to get the distinct element from mediastore by java

落爺英雄遲暮 提交于 2019-12-13 05:43:36

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/40062224/how-to-get-the-distinct-element-from-mediastore-by-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!