I can use below code to get current ringtone of incoming call
Uri defaultRintoneUri = RingtoneManager.getActualDefaultRingtoneUri(
getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
defaultRingtone = RingtoneManager.getRingtone(getApplicationContext(),
defaultRintoneUri);
But I want to get custom ringtone which is set by other application. How can I get it?
If the ringtone is used only for that application, then probably that ringtone file is private and owned only by that application. You won't have access to it, this is the security system on Android. Every app runs basically as a distinct Linux user.
On the other hand, if the ringtone is set for the ringtone system then that file is shared between all apps. In that case the file is stored in a public folder and the RingtoneManager will give you the current ringtone correctly.
I found the answer. Actually, Ringtone Manager will give us default ringtone of Android device. But if ringtone was changed by other application and we want to get it. We need the permission
READ_EXTERNAL_STORAGE
Let me give us an example :
Uri defaultRintoneUri = RingtoneManager.getActualDefaultRingtoneUri(
getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
defaultRingtone = RingtoneManager.getRingtone(getApplicationContext(),
defaultRintoneUri);
Without READ_EXTERNAL_STORAGE permission : above code will return default ringtone of Android device
But if other application change custom ringtone with the song like "Banana.mp3" (Minion - you known that right? lol)
And with READ_EXTERNAL_STORAGE permission : you will receive exactly Banana song
Without READ_EXTERNAL_STORAGE permission : default ringtone of Android device.
You have to insert that custom ringtone in the database of Media.
Try this code It will solve your issue.
File path = new File(path, "mysong.mp3"); // Path of your custom ringtone file.
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, path.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "My Song title"); // Title
values.put(MediaStore.MediaColumns.SIZE, 215454); // Size of file
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3"); // File Format
values.put(MediaStore.Audio.Media.ARTIST, "Madonna");
values.put(MediaStore.Audio.Media.DURATION, 230);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, false); // Default is false
values.put(MediaStore.Audio.Media.IS_MUSIC, false); // Default is false
//Insert the whole content in the Media database.
Uri uri = MediaStore.Audio.Media.getContentUriForPath(path.getAbsolutePath());
Uri newUri = main.getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(
myActivity,
RingtoneManager.TYPE_RINGTONE,
newUri);
GOOD LUCK
来源:https://stackoverflow.com/questions/41072204/get-custom-ringtone-incoming-call-of-android