1

I need to have URI of the default ringtone.

I can have the default ringtone using this code

            Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
            Ringtone rt = RingtoneManager.getRingtone(context,uri);

and in the rt (Ringtone), I can see the mUri like in the screenshot but it is not a public property, so I cannot have it.

How can I have that property?

Note: There is a getUri in the RingtoneManager.java but it is hidden.

/** {@hide} */
public Uri getUri() {
    return mUri;
}

Screen Shot

MareCieco
  • 157
  • 1
  • 19
  • Possible duplicate of [How to get current ringtone in Android?](https://stackoverflow.com/questions/22503189/how-to-get-current-ringtone-in-android) – ADM Mar 15 '18 at 09:44
  • Not duplicate. I need something else. – MareCieco Mar 15 '18 at 09:57
  • You can use reflection to access the `mUri` field – Arpan Sarkar Mar 15 '18 at 10:02
  • What are you asking, exactly? That `mUri` field is the `Uri` you just passed into the `RingtoneManager.getRingtone()` call. – Mike M. Mar 15 '18 at 10:08
  • @Arpanßløødyßadßøy I don't know how I can use the reflection. Can you give me a sample? – MareCieco Mar 15 '18 at 10:08
  • @MikeM. No mike, I pass content://settings/system/ringtone as uri but I need the physical path of the system ringtone. So I need content://media/internal/audio/media/110 – MareCieco Mar 15 '18 at 10:10
  • Well, that's not a physical path. It's a content URI. Anyway, following the source, it should be exactly what you just passed: https://android.googlesource.com/platform/frameworks/base/+/master/media/java/android/media/RingtoneManager.java#733, https://android.googlesource.com/platform/frameworks/base/+/master/media/java/android/media/Ringtone.java#284. – Mike M. Mar 15 '18 at 10:18
  • 1
    In any case, as Arpan ßløødy ßadßøy suggested, you can get that field with reflection: `Field f = Ringtone.class.getDeclaredField("mUri");`, `f.setAccessible(true);`, `Uri uri = (Uri) f.get(rt);`. See what that gives you. – Mike M. Mar 15 '18 at 10:21
  • @MikeM. Many Thanks – MareCieco Mar 15 '18 at 10:39

3 Answers3

2
Uri defaultRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(getActivity().getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
Ringtone defaultRingtone = RingtoneManager.getRingtone(getActivity(), defaultRingtoneUri);

Hope this works for you ! :)

Vivek Bhardwaj
  • 530
  • 5
  • 16
1
public void RingtonesList() {
  RingtoneManager manager = new RingtoneManager(this);
  manager.setType(RingtoneManager.TYPE_RINGTONE);
  Cursor cursor = manager.getCursor();
  while (cursor.moveToNext()) {
    String title = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
    String uri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX);
    // Do something with the title and the URI of ringtone
Log.d("URI",""+uri);
  }
}

Also give permission

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
Manish
  • 9
  • 1
  • 4
  • This returns content://media/internal/audio/media but I need to have content://media/internal/audio/media/110 – MareCieco Mar 15 '18 at 09:54
0
String mimeType;
        if (outPath.endsWith(".m4a")) {
            mimeType = "audio/mp4a-latm";
        } else if (outPath.endsWith(".wav")) {
            mimeType = "audio/wav";
        } else {
            // This should never happen.
            mimeType = "audio/mpeg";
        }    
ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, outPath);
        values.put(MediaStore.MediaColumns.TITLE, title.toString());
        values.put(MediaStore.MediaColumns.SIZE, fileSize);
        values.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(outPath);
        final Uri newUri = getContentResolver().insert(uri, values);

I am using this to set a file as ringtone for my phone, similar to Toque123, Tonos123, Sonneries123, Belsoli123, Chakushinon123

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 14 '23 at 13:45