First of all, let's talk about the Android multimedia database. Mediastore This class is a multimedia database provided by the Android system, which can be extracted from the Android multimedia information. This mediastore includes all the information in the multimedia database, including the audio
, video and images, Android put all the multimedia database interface encapsulated, all the databases do not have to create their own, the direct call to take advantage of the contentresolver removed with those packaged interface can be used for database operations. Today I'll introduce some
The use of these interfaces.
First, to get a contentresolver instance, Contentresolver can take advantage of an activity or service context. As shown below:
Contentresolver mresolver = Ctx.getcontentresolver ();
The top of the CTX is a context,activity.this is that context, this context is equivalent to a contextual environment. Once you get this context, you can call the Getcontentresolver interface to get the Contentresolver instance.
Contentresolver instances obtained, you can make a variety of queries, the following I take the audio database as an example to explain the method of adding and deleting, video and image and audio very similar.
Before explaining the various queries, I'll show you how to see which multimedia tables are available on Android. In the ADB shell, locate the/data/data/com.android.providers.media/databases/, and then locate the SD card database file (typically
A. db file), and then enter the command sqlite3 the name of the database to query the Android multimedia database. The table command lists all the tables for the multimedia database. Scheme plus the table name can query all column names in the table. Here you can benefit
Use SQL statements to see the data you want, and remember to keep in mind that each statement is preceded by a semicolon. Here's how to add and revise these tables.
Query, the code looks like this:
cursor cursor = resolver.query (_uri, Prjs, selections, Selectargs, order);
The Contentresolver Query method accepts several parameters with the following parameter meanings:
URI: This URI represents the name of the database to be queried plus the name of the table. This URI is generally obtained directly from the Mediastore, for example, I want to take all the information of the song, I must use the MediaStore.Audio.Media. EXTERNAL _content_uri this URI.
Album information to use MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI this URI to query, and other queries are similar.
PRJS: This parameter represents the column to select from the table, represented by a string array.
Selections: equivalent to the WHERE clause in the SQL statement, which represents your query criteria.
Selectargs: Is this the argument that you have in your selections? This symbol is where the actual value can be substituted for this question mark. What if selections this? , then this string array can be null.
Order: Describes what the query results are sorted by.
Above is the meaning of each parameter, it returns the query results a cursor, the cursor is equivalent to the database query result, usage and it is similar.
--------------------------------------------------------------------------------------------------------------- ----
Add the code as follows:
Contentvalues values = new Contentvalues ();
Values.put (mediastore.audio.playlists.members.play_order,0);
Resolver.insert (_uri, values);
This insert passes only two parameters, one is the URI (the same URI as the query), and the other is contentvalues. This contentvaluses corresponds to a row of data in the database, as long as the put method to set each column, directly using the insert side
It's good to have the law inserted.
Updated with the following code:
Contentresolver resolver = Ctx.getcontentresolver ();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Contentvalues values = new Contentvalues ();
Values.put (MediaStore.Audio.Media.DATE_MODIFIED, sid);
Resolver.update (Mediastore.audio.playlists.external_content_uri,values, where, Selectionargs);
The above Update method and query also has an increase in the parameters are very similar, here will not repeat the narrative, you can also directly refer to Google's documentation, there is also written clearly.
Delete the code as follows:
Contentresolver resolver = Ctx.getcontentresolver ();
nbsp Resolver.delete (Mediastore.audio.playlists.external_content_uri,where, Selectionargs);
Delete and update the method is very similar, we look at the Update method immediately will understand, today wrote here is busy recently, intends to send a piece of experience in a week, I hope we also write their own development experience to more exchanges.
Android Multimedia Database (non-original)