Android/Java: How to remove the filetype extention from a URI?
So I am working with URIs in Android to set ringtones for individual contacts. Problem is, the way the ringtone selector works is that it doesn't like something such as myringtone.mp3
. Instead, it just wants myringtone
. Weird, but I guess that's just how it goes. So I have a URI like file:///sdcard/tag/ringt开发者_StackOverflow中文版ones/ringtone.mp3
. How would I get the URI to remove the extension so the ringtone manager uses it properly? The way ringtones are set are with this line:
values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, uri.toString());
So the URI is in string form. Using substrings I should be able to do it, but I don't know how I'd write a method to remove the final 4 characters in a string. Can anyone give me some advice here?
Thanks!
For situations where you don't know what the extension may be or unknown length, try:
uri.toString().substring(0, uri.getString().lastIndexOf("."));
Have you tried?...
uri.toString().substring(0, uri.toString().length() - 4);
精彩评论