Android以编程方式设置铃声而无需更改ID

时间:2018-12-13 13:00:43

标签: android ringtone

我有一个问题。我在stackoverflow上找到了set the song as a ringtone的一种方法。有用。但是这种方式改变了歌曲ID。有必要吗?我关心不更改它。这是代码:

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);
}

您怎么看?是否可以不删除它?

祝您愉快先谢谢您的帮助。

0 个答案:

没有答案