question

johncanyon-2528 avatar image
0 Votes"
johncanyon-2528 asked johncanyon-2528 edited

need to change default ringtone with xamarin



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






dotnet-csharpdotnet-xamarinformsdotnet-androiddotnet-xamarinessentials
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MediaStore.IMediaColumns.Data is deprecated, please use values1.Put(MediaStore.Audio.Media.InterfaceConsts.RelativePath, "/mnt/sdcard/Music/jay.mp3");

`

0 Votes 0 ·

it need to be Android.Net.Uri type so i can use it on

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

know any way of how to do it?

0 Votes 0 ·

values.Put(MediaStore.IMediaColumns.Data, ring1); is drepcated, please refer to this thread to create a new code:https://stackoverflow.com/questions/58255532/how-to-set-a-file-as-a-ringtone-for-android-10/59309578#59309578

0 Votes 0 ·

1 Answer

johncanyon-2528 avatar image
0 Votes"
johncanyon-2528 answered johncanyon-2528 edited

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


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.