I'm currently trying to select a file with an Intent. My Problem is, that the path returned is not in the right format.
My Intent:
private void selectAudioFile(object sender, EventArgs eventArgs)
{
Intent = new Intent();
Intent.SetType("audio/*");
Intent.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(Intent, "Select Audio File"), PickAudioId);
}
And the rusult method:
protected override void OnActivityResult (int requestCode, Result resultCode, Intent data) {
base.OnActivityResult (requestCode, resultCode, data);
if ((resultCode == Result.Ok) && (requestCode == PickAudioId) && (data != null)) {
Android.Net.Uri uri = data.Data;
if (!File.Exists(uri)) {
// error
}
}
}
The Problem:
I need to handle the received path with the File class. The path looks like /document/audio:1234. If I check the path with File.Exists(uri) it sais that the file does not exist.
How can i get the path to this file in a format i can handle with File like /storage/emulated/0/Music/foo.mp3 or something like that ?
Thanks for help
The uri you get in return may look like a path but it's not, it's more like a shortcut to a database record.
To retrieve a real path is quite a mouthful, you've got to use a ContentResolver, here's how to retrieve it (based on that sample from Xamarin) :
you can also add other Android.Provider.MediaStore.Audio.Media.InterfaceConsts.XXX values to get more info about the file
++