从特定路径获取所有歌曲

时间:2015-05-20 16:02:26

标签: android mediastore

我正在尝试从特定文件夹中获取所有歌曲。目前,我正在获取所有歌曲,然后根据路径我循环并获取特定路径中的歌曲。更好的方法来做到这一点?

 for (int i = 0; i < totalSongList.size(); i++) {
        String path = totalSongList.get(i).getPathId();
        int index = path.lastIndexOf("/");
        String folderPath1 = path.substring(0, index);

        if (folderPath.equals(folderPath1))
            songList.add(totalSongList.get(i));

    }

我也不能使用下面的代码,因为它也会获取子文件夹歌曲。

musicResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,MediaStore.Audio.Media.DATA + " like ? ",
        new String[] {"%SPECIFIC_FOLDER_NAME%"},  null);

1 个答案:

答案 0 :(得分:0)

以下是不使用mediaStore检索特定文件夹中可能包含或不包含任何子文件夹的所有歌曲的java代码。

public String File path = new File("your folder path here");

public void Search(File dir)
{
if(dir.isFile())
{
//get necessary songName over here
String songName = dir.getName();

//if you need path
String songPath = dir.getAbsolutePath();

//if your folder consists of other files other than audio you have to filer it 

if(songName.endsWith(".mp3") || songName.endsWith(".MP3")
{
//this is the required mp3 files
//do what you want to do with it
}

}else if(dir.isDirectory())
{
File[] list = dir.listFiles();

for(int i=0;i<list.length();i++)
{
search(list[i]);
}

}
else
{
//handle exceptions here
}     


}
相关问题