How to obtain music file information via Mediastore in Android _android

Source: Internet
Author: User
Tags int size

The Android system provides interfaces such as Mediascanner,mediaprovider,mediastore, and provides a set of database tables that are available to users through content provider. When the cell phone boot or SD card Plug and other events occur, the system will automatically scan the SD card and cell phone memory of the media files, such as audio,video, pictures, etc., the corresponding information into a well-defined database table. In this program, we do not need to care about how to scan the phone files, as long as you know how to query and use this information.

A series of data tables are defined in Mediastore, and we can get all the information we need through the query interface provided by Contentresolver. Below we focus on the query SD card music file information.
Let's take a look at the Contentresolver query interface:

Copy Code code as follows:

Cursor query (Uri uri, string[] projection, string selection, string[] Selectionargs, string sortOrder);

Uri: Indicates the name of the database to query, plus the name of the table, from Mediastore we can find the parameters of the corresponding information, please refer to the development documentation.
Projection: Specifies which columns in the Query database table, and the corresponding information is included in the returned cursor. NULL returns all the information.
Selection: Specifying query criteria
Selectionargs: Is there a parameter in selection? The symbol here is that the actual value can be substituted for this question mark. What if selection this? , then this string array can be null.
SortOrder: Specify the order in which query results are ordered
The following command returns information about all the music files on the external storage card:
Copy Code code as follows:

Cursor Cursor = query (MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, NULL, NULL, NULL, MediaStore.Audio.Media.DEFAULT_ Sort_order);

After getting the cursor, we can call the relevant method of cursor specific music information:
Song id:mediastore.audio.media._id
Copy Code code as follows:

Int id = cursor.getint (cursor.getcolumnindexorthrow (mediastore.audio.media._id));

Name of Song: MediaStore.Audio.Media.TITL

Copy Code code as follows:

String Tilte = cursor.getstring (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.TITLE));

Album Name of Song: MediaStore.Audio.Media.ALBUM

Copy Code code as follows:

String album = cursor.getstring (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.ALBUM));

Singer name of the song: MediaStore.Audio.Media.ARTIST

Copy Code code as follows:

String artist = cursor.getstring (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.ARTIST));

Full path to song file: MediaStore.Audio.Media.DATA

Copy Code code as follows:

String url = cursor.getstring (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.DATA));

Name of song file: MediaStroe.Audio.Media.DISPLAY_NAME
Copy Code code as follows:

String display_name = cursor.getstring (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.DISPLAY_NAME));

Song File release date: MediaStore.Audio.Media.YEAR

Copy Code code as follows:

String year = cursor.getstring (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.YEAR));


Total playback length of the song: MediaStore.Audio.Media.DURATION

Copy Code code as follows:

Int Duration = Cursor.getint (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.DURATION));

Size of song file: MediaStore.Audio.Media.SIZE

Copy Code code as follows:

Int size = Cursor.getlong (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.SIZE));

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.