Search code examples
androidbackupringtone

How can I check if a Ringtone exists?


In my app, I'm storing the user's choice of a ringtone in a SharedPreference file. When the app is reinstalled and the backup is restored, I want to check if the ringtone still exists on the device, because if it doesn't I would want to use the default ringtone (as opposed to playing nothing).

So to do so, I plan on overriding the onRestore method and checking if the ringtone is available on the device.

So how can I go about checking if a ringtone exists on the Android device (I would prefer the most lightweight method possible)?


Solution

  • Assuming that you are storing the ringtone as a content:// Uri value, I would use either openInputStream() or getType() on ContentResolver. getType() is probably "the most lightweight", but it might be prone to false negatives (e.g., ringtone exists but the MIME type cannot be determined for some reason).