MediaStore research on Android multimedia database

Source: Internet
Author: User

At the request of netizens, I would like to tell you about the android multimedia database today. 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 I 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. Here, you can use SQL statements to view the data you want. Remember to add a score after 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 can check the update method and understand it immediately. Today, I am writing it here. I plan to share my experiences in one week, I hope you can share your development experience with us.

Related Article

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.