In my application I need the user to select a ringtone as an application sound. I use RingtoneManager.ACTION_RINGTONE_PICKER
to get the selected ringtone. It returns the ringtone's Uri
, and I can turn that into a ringtone and it all works just fine. But here's the tricky part:
All my application data is kept in an XML file, so I need a way to save the ringtone as a String
.
I was thinking of just getting the ringtone's path using Uri.getPath()
and saving the path in my app. But I can't find a way to convert the path back to a ringtone when I need to use it.
Here's the code I tried to convert a Uri to String and back:
String ringtonePath = ringtoneUri.getPath();
ringtoneUri = Uri.parse(ringtonePath);
Ringtone ringtone = RingtoneManager.getRingtone(getActivity(), ringtoneUri);
It doesn't work. LogCat outputs:
Failed to open ringtone /internal/audio/media/13
and ringtone gets null
Uri.path
only returns the path
part of the URL. For instance, if the URI is "http://hello.com/world/1", then path
is only the "/world/1" part of the URI. You should store the result of Uri.toString()
in your XML instead.