本文介绍了如何通过Java从MediaStore获取独特元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取所有音频文件的文件名,但是多首歌曲却获得相同的文件名

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

1.我无法使用DISTINCT关键字,因为我是从DATA获取文件名的.

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

2.我正在使用Mediastore.Files,因此选择需要MEDIA_TYPE,所以这种方式也是不可能的.

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

3.我想获得Parent值,使其与众不同而不是重复值.

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

所以唯一的方法是在Java中进行操作.我遵循了,但我无法设置

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

这是我的一部分代码

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());
}

如何获取非重复元素??有关更多信息,请 mediastore.file 我正在Mediafileinfo类中添加包含getter和setter的数据.

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

推荐答案

好的,您可以使用HashSet<String>维护可见的MediaStore.Files.FileColumns.PARENT值的列表.

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

不过,不确定SQL方法有什么问题.

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

这篇关于如何通过Java从MediaStore获取独特元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 06:55