Android: Save android.net.Uri object to Database

Sammy picture Sammy · Jun 19, 2011 · Viewed 9.3k times · Source

What i m trying to do is get the selected ringtone from the user, set an AlarmManager alarm to play that ringtone when the alarm goes Off. but I need to save the ringtone in the database so I can reset all the alarms after phone reboot.

my question is what is the best way to save the ringtone Uri to the database to retrieve later?

I tried the followings:

1) save the ringtone title in the DB and then retrieve it and append it to a default ringtone path. but the issue, is that the ringtone might be loaded from a different location 2) Storing the uri scheme, scheme spefici part and fragment and then call Uri.fromParts to create the Uri. 3) create an inputStream, byte[] array from the Uri and saving it as a blob and then reading it back and cast the result to Uri

none of these works.

Your help is much appreciated.

Answer

Srichand Yella picture Srichand Yella · Jun 19, 2011

Store the URI as a string in the database and then load it later.

// This will get the uri in a string format
String s = mUri.toString();

When you retrieve the string from the database, rebuild the URI like this:

// This will decode the string into a URI
Uri mUri = Uri.parse(s);

Hope that helps. Good luck!