Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Save android.net.Uri object to Database

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.

like image 679
Sammy Avatar asked Jun 19 '11 18:06

Sammy


People also ask

What does URI Parse do in Android?

A Uri object is usually used to tell a ContentProvider what we want to access by reference. It is an immutable one-to-one mapping to a resource or data. The method Uri. parse creates a new Uri object from a properly formated String .

What is Android Net URI?

Immutable URI reference. A URI reference includes a URI and a fragment, the component of the URI following a '#'. Builds and parses URI references which conform to RFC 2396. In the interest of performance, this class performs little to no validation.

What is RoomDatabase?

RoomDatabase provides direct access to the underlying database implementation but you should prefer using Dao classes. See also: Database.


1 Answers

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!

like image 179
Srichand Yella Avatar answered Oct 01 '22 23:10

Srichand Yella