2

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Khoa Tran
  • 528
  • 6
  • 18
  • I think you are looking for the path of the current Ringtone but not at the time when the phone rings. Please correct me if I am wrong. – Shubham Dec 12 '16 at 11:00
  • Thanks @Shubham but I still don't get you. Let me give you an example: - I download an application can set incoming call ringtone - Then I want my application can get that ringtone uri or path of file. – Khoa Tran Dec 12 '16 at 11:12
  • `Uri defaultRintoneUri = RingtoneManager.getActualDefaultRingtoneUri( getApplicationContext(), RingtoneManager.EXTRA_RINGTONE_EXISTING_URI);` Try to use this. – Shubham Dec 12 '16 at 11:19
  • hmm the function getActualDefaultRingtoneUri is public static Uri getActualDefaultRingtoneUri(Context context, int type) but RingtoneManager.EXTRA_RINGTONE_EXISTING_URI is a String bro. – Khoa Tran Dec 12 '16 at 11:21
  • Sorry my bad. `Uri defaultRingtoneUri = Uri.parse(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI);` – Shubham Dec 12 '16 at 11:28
  • Thanks bro but it still doesn't work :( – Khoa Tran Dec 12 '16 at 12:08
  • it is already mentioned in another question's answer. You can simply share the same link too in the comments instead of copying answer http://stackoverflow.com/questions/22503189/how-to-get-current-ringtone-in-android – VVB Dec 19 '16 at 08:59
  • lol I didn't copy the answer but it's up to you. I just want to make everything clear and I'm not sure you read the question or not before answer. – Khoa Tran Dec 19 '16 at 09:08

3 Answers3

0

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.

0

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.

Khoa Tran
  • 528
  • 6
  • 18
-1

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

Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40
  • How about other application insert their custom ringtone so we can not get it? Please read my question clearly, thanks. – Khoa Tran Dec 10 '16 at 05:50