好的,我和你有同样的问题。我假设您传入的Uri是您资产中的文件,或者是您存储在首选项文件中的磁盘。在这个假设下工作,问题是你不能将Uri推入RingtoneManager并期望它接受它。文件uri应该来自内容解析器。
我很乐意,如果有人能告诉我为什么会这样,但我不是专家,所以我现在就接受它。话虽这么说,这段代码将让你采取一个Uri并将其设置为默认铃声。
//We get the Uri here fro ma file's absolute path.
Uri ringtoneUri = Uri.parse(file.getAbsolutePath());
//We now create a new content values object to store all the information
//about the ringtone.
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, chosenFile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, chosenFile.getName());
values.put(MediaStore.MediaColumns.SIZE, chosenFile.length());
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(AudioColumns.ARTIST, context.getString(R.string.app_name));
values.put(AudioColumns.IS_RINGTONE, true);
values.put(AudioColumns.IS_NOTIFICATION, false);
values.put(AudioColumns.IS_ALARM, false);
values.put(AudioColumns.IS_MUSIC, false);
//Work with the content resolver now
//First get the file we may have added previously and delete it,
//otherwise we will fill up the ringtone manager with a bunch of copies over time.
Uri uri = MediaStore.Audio.Media.getContentUriForPath(chosenFile.getAbsolutePath());
context.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + chosenFile.getAbsolutePath() + "\"", null);
//Ok now insert it
Uri newUri = context.getContentResolver().insert(uri, values);
//Ok now set the ringtone from the content manager's uri, NOT the file's uri
RingtoneManager.setActualDefaultRingtoneUri(
context,
RingtoneManager.TYPE_RINGTONE,
newUri
);