我无法将音频文件设置为旧版本设备的铃声。我为新 android 版本编写的部分工作正常

I cannot set the audio file as ringtone for older version devices. The section I wrote for new android versions works fine

代码:

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.TITLE, k.getName());
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
if (RingtoneManager.TYPE_RINGTONE == type) {
    values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
} else if (RingtoneManager.TYPE_NOTIFICATION == type) {
    values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
}
values.put(MediaStore.MediaColumns.SIZE,k.length());
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + """ + k.getAbsolutePath() + "\"", null);
System.out.println("Uri string: "+uri.toString());  //Do you think the output I got here is normal? Output = content: // media / internal / audio / media
Uri newUri = setandsharejava.this.getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(setandsharejava.this,type,newUri);

k=文件

铃声设置为我想要的名字,但播放时使用android的默认声音。

没有错误信息。

感谢您的关注。

做我评论的标题: How to set ringtone in Android from my activity? Set a selected audio file as ringtone Set a sound file as ringtone and notification programmatically in Android How can I change the ringtone in android programmatically? Set ringtone from res/raw folder How can I set a ringtone for an individual contact on Android?

还有更多...

经过努力,我解决了这个问题。 问题是我试图从应用程序的缓存中获取音频文件。 将音频文件刻录到 sdcard 分区后,它工作正常。