Hello,
Welcome to our Microsoft Q&A platform!
When I use MediaStore.Audio.Media.InterfaceConsts.RelativePath
I get an exception about RelativePath do not existed in android 9.0 emulator, then I change it to MediaStore.Audio.Media.InterfaceConsts.Data
like following code.
public List<string> GetAudioFile()
{
List<string> vs = new List<string>();
Context context= Android.App.Application.Context;
string[] columns = {
MediaStore.Audio.Media.InterfaceConsts.IsMusic,
MediaStore.Audio.Media.InterfaceConsts.Data,
MediaStore.Audio.Media.InterfaceConsts.Title,
MediaStore.Audio.Media.InterfaceConsts.Album,
MediaStore.Audio.Media.InterfaceConsts.AlbumId,
MediaStore.Audio.Media.InterfaceConsts.Artist,
MediaStore.Audio.Media.InterfaceConsts.ArtistId,
};
Log.Info("Test", "Query start");
ICursor cursor = context?.ContentResolver?.Query(MediaStore.Audio.Media.ExternalContentUri, columns, null, null, null);
if (cursor is null)
{
Log.Info("Test", "null");
}
while (cursor.MoveToNext())
{
Log.Info("Test", "next");
//if (!Boolean.Parse(cursor.GetString(0)))
//{
// Log.Info("Test", "Not music");
// continue;
//}
var Path= cursor.GetString(cursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Data));
string trackPath = cursor.GetString(1);
vs.Add(trackPath);
Log.Info("Test", trackPath);
}
Log.Info("Test", "Query end");
return vs;
}
Note: If you want to read external file, you need to add following permission.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
And grand the permission at the runtime.
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//Open read permission.
if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) != (int)Permission.Granted)
{
RequestPermissions(new string[] { Manifest.Permission.ReadExternalStorage}, 0);
}
}
}
==================
Update code===================
Here is my updated code.
public List<string> GetAudioFile()
{
List<string> vs = new List<string>();
Context context= Android.App.Application.Context;
string[] columns = {
MediaStore.Audio.Media.InterfaceConsts.IsMusic,
MediaStore.Audio.Media.InterfaceConsts.RelativePath,
MediaStore.Audio.Media.InterfaceConsts.DisplayName,
MediaStore.Audio.Media.InterfaceConsts.Title,
MediaStore.Audio.Media.InterfaceConsts.Album,
MediaStore.Audio.Media.InterfaceConsts.AlbumId,
MediaStore.Audio.Media.InterfaceConsts.Artist,
MediaStore.Audio.Media.InterfaceConsts.ArtistId,
};
Log.Info("Test", "Query start");
ICursor cursor = context?.ContentResolver?.Query(MediaStore.Audio.Media.ExternalContentUri, columns, null, null, null);
if (cursor is null)
{
Log.Info("Test", "null");
}
while (cursor.MoveToNext())
{
Log.Info("Test", "next");
var Path= cursor.GetString(cursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.RelativePath));
string MP3Name = cursor.GetString(cursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.DisplayName));
string trackPath = Path + MP3Name;
vs.Add(trackPath);
Log.Info("Test", trackPath);
}
Log.Info("Test", "Query end");
return vs;
}
Best Regards,
Leon Lu
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.