setringtoneringtonemanager

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


Code:

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=File

The ringtone is set with the name I want but when playing it uses the default sound for android.

There is no error message.

Thank you for your interest.

Do titles I reviewed: How to set ringtone in Android from my activity? Set a selected audio file as ringtone How to set a file as a ringtone for Android 10? 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?

and more...


Solution

  • I solved the problem after trying hard. The problem was that I was trying to get the audio file from the application's cache. After burning the audio file to the sdcard partition it worked fine.