Research on the Mediastore of Android multimedia database

Source: Internet
Author: User

At the request of netizens, today we will talk about the multimedia database of Android. 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 of the multimedia database, including audio, video and images, and Android encapsulates all of the multimedia database interfaces, all of which are not created by themselves. Direct calls take advantage of Contentresolver to eliminate the use of those encapsulated interfaces to perform database operations. Today I'll introduce some of these interfaces to use.

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/data/data/com.android.providers.media/databases/, and then locate the SD card database file (typically a. db file), Then enter the command sqlite3 with the name of the database to query the Android multimedia database. The table command lists all the multimedia database tables, and the. Scheme plus the table name can query all column names in the table. Here you can use the SQL statement to see the data you want, and remember to keep in mind that each statement is followed 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 method to insert the good.

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 ();

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.