Search code examples
javaandroiduriringtone

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.


Solution

  • 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