Home > Software design >  need to change default ringtone with xamarin
need to change default ringtone with xamarin

Time:09-23

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

CodePudding user response:

You need to recheck the path of ring1.

I use the file in download folder for reference. It works.

 var ring1 = System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads, "sample.mp3");

If you want to use the default Uri of Ringtone, you could use the code below instead.

  RingtoneManager.GetDefaultUri(RingtoneType.Ringtone)

CodePudding user response:

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

  • Related