Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if ringtone pointed by uri exists?

I'm able to play a ringtone using the following code

rone = RingtoneManager.getRingtone(this.getContext(), Uri.parse(one));
rone.play();

How do I check if the audio file pointed by uri exists? When the user deletes a file from their phone, I'm not able to play the ringtone and it crashes.

like image 253
Chris Rohit Brendan Avatar asked Apr 19 '13 17:04

Chris Rohit Brendan


2 Answers

getRingtone() will return null if the ringtone does not exist.

Always check that a returned value is not null, this is a good practice whenever you call methods that might return null value. This will prevent a whole lot of NullPointerException

like image 71
tbkn23 Avatar answered Nov 13 '22 07:11

tbkn23


Try to use getRingtonePosition() method instead of getRingtone():

final RingtoneManager rm = new RingtoneManager(preference.getContext());
final Uri loadedRingtoneUri = Uri.parse(ringtoneUriString);
final int pos = rm.getRingtonePosition(loadedRingtoneUri);

// if loaded ringtone uri is valid, use it or use default
final Ringtone ringtone = RingtoneManager.getRingtone(preference.getContext(),
                        (pos != -1) ? loadedRingtoneUri :
                                      Settings.System.DEFAULT_RINGTONE_URI);
like image 22
Maksim Diakonov Avatar answered Nov 13 '22 07:11

Maksim Diakonov