Android multimedia database mediastore

Source: Internet
Author: User

Mediastore is a multimedia database provided by the Android system. Multimedia Information in android can be extracted from this class. This mediastore includes all the information of the multimedia database, including audio, video, and image. Android encapsulates all the multimedia database interfaces, and all databases do not need to be created by themselves, you can directly call contentresolver to remove the encapsulated interfaces and perform database operations. Today we will introduce some usage of these interfaces.

First, you need to obtain a contentresolver instance. contentresolver can obtain the instance by using the context of an activity or service. As follows:

Contentresolver mresolver = CTX. getcontentresolver ();

The CTX above is a context, and activity. This is the context, which is equivalent to a context. After obtaining this context, you can call the getcontentresolver interface to obtain the contentresolver instance. After obtaining the contentresolver instance, you can perform various queries. Next I will use the audio database as an example to explain how to add, delete, modify, and query. Videos and images are very similar to audios.

Before explaining various queries, I would like to explain how to see which multimedia tables are provided by Android. In the ADB shell, find/data/COM. android. providers. media/databases/, and then find the database file of the SD card (usually one. database file), and then enter the command sqlite3 plus the name of the database to query the android multimedia database .. The table command can list the tables of all multimedia databases. scheme and table names can be used to query all column names in the table. You can use SQL statements to view the data you want. Remember to add a semicolon to each statement. The following describes how to add, delete, modify, and query tables.

The Code is as follows:

Cursor cursor = resolver. Query (_ Uri, prjs, selections, selectargs, order );

The contentresolver query method accepts several parameters, which have the following meanings:

Uri: indicates the name of the database to be queried plus the name of the table. This URI is generally obtained directly from mediastore. For example, to retrieve information about all songs, you must use the URI mediastore. Audio. Media. External _ content_uri. The album information should be queried using the URI mediastore. Audio. albums. external_content_uri. Other queries are similar.

Prjs: this parameter represents the columns to be selected from the table, represented by a string array.

Selections: equivalent to the WHERE clause in an SQL statement, which represents your query conditions.

Selectargs: What does this parameter mean in your selections? This symbol is used to replace the question mark with the actual value. If selections does not exist? Then, the string array can be null.

Order: sort the query results.

The above is the meaning of each parameter. The returned query result is a cursor, which is equivalent to the result in the database query. Its usage is similar to that in it.

The Code is as follows:

Contentvalues values = new contentvalues ();

Values. Put (mediastore. Audio. playlists. members. play_order, 0 );

Resolver. insert (_ Uri, values );

There are only two parameters passed in this insert statement. One is uri (the URI to be queried) and the other is contentvalues. This contentvaluses corresponds to a row of data in the database. You only need to use the put method to set each column and use the insert method to insert it directly.

Update the Code as follows:

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 is similar to the parameters added to the query, so we will not repeat them here. You can also directly refer to the Google documentation, which is also clearly written.

Delete, the Code is as follows:

Contentresolver resolver = CTX. getcontentresolver ();

Resolver. Delete (mediastore. Audio. playlists. external_content_uri, where, selectionargs );

The Delete and update methods are similar. You will understand the update method immediately.

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.