Во-первых, вам нужно добавить разрешение для вашего проекта.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
в коде за
List<Dictionary<string, string>> GetPlayList(string rootPath)
{
List<Dictionary<string, string>> fileList = new List<Dictionary<string, string>>();
try
{
File rootFolder = new File(rootPath);
File[] files = rootFolder.ListFiles(); //here you will get NPE if directory doesn't contains any file,handle it like this.
foreach (var file in files)
{
if (file.IsDirectory)
{
if (GetPlayList(file.AbsolutePath) != null)
{
fileList = new List<Dictionary<string, string>>(GetPlayList(file.AbsolutePath));
}
else
{
break;
}
}
else if (file.Name.EndsWith(".mp3"))
{
Dictionary<string, string> song = new Dictionary<string, string>();
song.Add("file_path", file.AbsolutePath);
song.Add("file_name", file.Name);
fileList.Add(song);
}
}
return fileList;
}
catch (Exception e)
{
return null;
}
}
И вы можете вызвать его как
List<Dictionary<string, string>> songList = GetPlayList("/storage/sdcard1/");