0

I have one question. I found on the stackoverflow one way to set the song as a ringtone. It works. But this way is changing song ID. Is it necessary? I care about not changing it. Here is code:

private void setSongAsRingtone(Song song){
    File ringtoneFile = new File(song.getPath());
    String filePathToDelete = MediaStore.MediaColumns.DATA + "=\"" + ringtoneFile.getAbsolutePath() + "\"";
    ContentValues content = getContentValuesOfTheSong(song, ringtoneFile);
    Uri uri = MediaStore.Audio.Media.getContentUriForPath(ringtoneFile.getAbsolutePath());

    getActivity().getContentResolver().delete(uri, filePathToDelete, null);
    setNewRingtone(uri, content);
}

private ContentValues getContentValuesOfTheSong(Song song, File ringtoneFile){
    ContentValues content = new ContentValues();
    content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
    content.put(MediaStore.MediaColumns.TITLE, song.getTitle());
    content.put(MediaStore.MediaColumns.SIZE, 215454);
    content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
    content.put(MediaStore.Audio.Media.ARTIST, song.getArtist());
    content.put(MediaStore.Audio.Media.DURATION, song.getDuration());
    content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
    content.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
    content.put(MediaStore.Audio.Media.IS_ALARM, true);
    content.put(MediaStore.Audio.Media.IS_MUSIC, true);
    return content;
}

private void setNewRingtone(Uri uri, ContentValues content){
    Uri newUri = getActivity().getContentResolver().insert(uri, content);
    RingtoneManager.setActualDefaultRingtoneUri(getActivity(),
            RingtoneManager.TYPE_RINGTONE, newUri);
}

What do you think? Is it possible not to delete it?

Have a nice day! Thanks in advance for the help.

Oktawian
  • 334
  • 1
  • 2
  • 16
  • 1
    Looks like it was already answered here: https://stackoverflow.com/questions/40420969/set-a-sound-file-as-ringtone-and-notification-programmatically-in-android/45836076#45836076 – yhyrcanus Dec 13 '18 at 22:06
  • yhyrcanus, that answer doesn't work – Oktawian Dec 14 '18 at 20:18

0 Answers0