need to change default ringtone with xamarin

johncanyon 201 Reputation points
2021-09-19T12:37:58.047+00:00

need to change default ringtone with xamarin i use android.net.parse with path string and return null, then i use the bellow code where i find online and again return null anybody can help?

var ring1 = ("file:////storage/emulated/0/Ringtones/" + item.ringtone);
     ContentValues values = new ContentValues();

                            values.Put(MediaStore.IMediaColumns.Data, ring1);
                            values.Put(MediaStore.Audio.Media.InterfaceConsts.IsRingtone, true);
                            values.Put(MediaStore.IMediaColumns.MimeType, "audio/mp3");



                            var uri = MediaStore.Audio.Media.GetContentUriForPath(path: ring1);


                            Android.Net.Uri newUri = this.ContentResolver.Insert(uri, values);

    RingtoneManager.SetActualDefaultRingtoneUri(Android.App.Application.Context,RingtoneType.Ringtone, newUri);

"uri" and "values" in debugging have values but parameter newUri is null

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,325 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,621 questions
{count} votes

Accepted answer
  1. johncanyon 201 Reputation points
    2021-09-22T17:28:46.137+00:00

    The problem was in write permisions i use the code below to check if the file exist ask for permision and change the ringtone and work

    var newuri3 = Android.Net.Uri.Parse("file:///storage/emulated/0/Ringtones/" + item.ringtone);
    
                        var newuri = newuri3;
                        bool b = false;
                        if (null != newuri)
                        {
                            try
                            {
                                var inputStream = Android.App.Application.Context.ContentResolver.OpenInputStream(newuri);
                                inputStream.Close();
                                Console.WriteLine("file exist");
                                b = true;
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine("File corresponding to the uri does not exist " + newuri.ToString());
                            }
                        }
    
                        var bi = Settings.System.CanWrite(Android.App.Application.Context);
                        if (bi)
                        {
                            Console.WriteLine("it haw write permision");
                        }
                        else
                        {
                            Console.WriteLine("doenst have permision");
    
    
    
                            Intent intent = new Intent(Settings.ActionManageWriteSettings);
                            intent.SetData(Android.Net.Uri.Parse($"package:{Android.App.Application.Context.PackageName}"));
                            intent.AddFlags(ActivityFlags.NewTask);
                            Android.App.Application.Context.StartActivity(intent);
    
                        }
    
    
    
                        RingtoneManager.SetActualDefaultRingtoneUri(Android.App.Application.Context, RingtoneType.Ringtone, newuri);
    

    where item.ringtone is the file name for example music.mp3

    0 comments No comments

0 additional answers

Sort by: Most helpful