Forum Xamarin Xamarin.Android

Xamarin.Android 10 Q how to store/access custom sound using MediaStore

Cdn_EuroCdn_Euro Member ✭✭✭
edited October 2019 in Xamarin.Android

My goal is to store a sound in MediaStore and then use that sound for push notifications.

I need some help using the MediaStore. Reading the documentation for Android Q I understand that I cannot just store a sound in External Storage like I did before (and it worked) and then use that sound for my push notifications channel. Now access to external storage is limited. I understand that one should use MediaStore instead. I am trying to store a sound in the MediaStore.

1. How do I copy the sound into the media store, because my method below is incomplete.
2. How do I retrieve/access tthat sound from the mediastore in order to set it as the default sound for a push notifications channel.Is the access still done via Uri...or by cursor?
3. How to set it as default sound for a notification channel.

This is my code so far:

// param filePath is the path of the file in the AppData Directory from Xamarin Essentials (cache directory the temporary location)
// and looks like: /data/user/0/
// I want to return a Uri of the file after inserted in the MediaStore if thats' possible in order to set it for the notification channel

public Android.Net.Uri InsertCustomSoundInMediaStore(string filePath)
            ContentValues values = new ContentValues();
            ContentResolver contentResolver = CrossCurrentActivity.Current.AppContext.ContentResolver;

            var appName = AppInfo.Name;

            values.Put(MediaStore.MediaColumns.Title, "customalert");
            values.Put(MediaStore.MediaColumns.MimeType, "audio/wav");
            values.Put(MediaStore.MediaColumns.Size, GetDuration(filePath));
            values.Put(MediaStore.Audio.Media.InterfaceConsts.Artist, appName);
            values.Put(MediaStore.Audio.Media.InterfaceConsts.IsRingtone, true);
            values.Put(MediaStore.Audio.Media.InterfaceConsts.IsNotification, true);
            values.Put(MediaStore.Audio.Media.InterfaceConsts.IsAlarm, true);
            values.Put(MediaStore.Audio.Media.InterfaceConsts.IsMusic, false);

            Android.Net.Uri newUri = contentResolver.Insert(MediaStore.Audio.Media.ExternalContentUri, values);   

                   // copy file from cache to OS (MediaStore)?
            catch (Exception ex)

            return newUri;

Best Answer


  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited October 2019

    If you want to store a sound in External Storage like you did before, you can add the android:requestLegacyExternalStorage = “true” to tab in AndroidManifest.xml file.

    If you need to know more details about it, you can refer to the following link.

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    @Cdn_Euro Do you have some update for this issue? If the reply is helpful, please mark it as an answer, it will help others who have similar issue.

  • Cdn_EuroCdn_Euro Member ✭✭✭

    @LeonLu havent had a chance to work on it following your advice, it’s Thanksgiving here. Thanks for giving me that info and I sure will apply it soon and mark the answer.

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Ok, waitting for your update.

  • Cdn_EuroCdn_Euro Member ✭✭✭

    @LeonLu Thanks so much it works, I have spend literally days on trying to figure this out.

  • Cdn_EuroCdn_Euro Member ✭✭✭

    @LeonLu There is one more issue though. I haven't had time to properly test it, but now I am back working at it. I do get the URI from the sound stored in the media store by the code above, but when trying to assign that URI as the default sound for my custom notification channel, it doesn't work, the sound does not play when notification comes in.

    Am I missing something? Again just to summarize, I am recording a sound which is stored in a temporary location (cache) and then I am grabbing that temporary path with the method above, and I want to store the file using the new method for Android Q ---> using MediaStore(so basically transfer the recording from the temporary path to the Media Store). I then want to grab the recording from MediaStore and play it when notification comes in, on my default notification channel. What is the new way to make this work on Android Q?

Sign In or Register to comment.