0

After the deprecation of getContentUriForPath() and MediaStore.MediaColumns.DATA i modified the code as below :

 private boolean SetAsRingtone(File k)
    {

        ContentValues values = new ContentValues();

        values.put(MediaStore.MediaColumns.TITLE, k.getName());
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
        values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
        values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
        values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
        values.put(MediaStore.Audio.Media.IS_ALARM, false);
        values.put(MediaStore.Audio.Media.IS_MUSIC, false);

        Uri newUri=this.getContentResolver()
                .insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);

        RingtoneManager.setActualDefaultRingtoneUri(Emotes.this, RingtoneManager.TYPE_RINGTONE,
                newUri);

        return true;
    }

The problem is that the ringtone of the device does not change. I can see from the sound setting that it changed but when the phone rings the default music plays.

Any idea how i can fix this?

Ryan M
  • 18,333
  • 31
  • 67
  • 74
ibrahim
  • 573
  • 1
  • 6
  • 20
  • I think the problem is that the URI returned by the audio media store doesn't have the ringtone file associated with it. As you say, the DATA column was deprecated, and it's no longer possible to insert a record in the audio media store with a path to the file. Instead, I believe you have to do the insert, then write the content of the file to the URI returned. Perhaps have a look at this link and see if it helps? https://developer.android.com/training/data-storage/shared/media#add-item – clownba0t Dec 11 '19 at 06:17
  • Did you resolve it @lbrahim? – Sinh Phan Apr 26 '21 at 16:59
  • @PhanSinh yes i have posted the answer in here https://stackoverflow.com/questions/58255532/how-to-set-a-file-as-a-ringtone-for-android-10/59309578#59309578 although its a bit outdated – ibrahim Apr 26 '21 at 18:00
  • @Ibrahim Thanks for your responding. I have got this error `Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/audio/media` My file have stored in this path: `File(context.getExternalFilesDir("ringtones"), "ringtone_123.mp3")` and I did allow the `WRITE_EXTERNAL_STORAGE` permission. My project have build on target api 30. Do you know what's problem there? – Sinh Phan Apr 27 '21 at 06:30

0 Answers0