The Android system provides MediaScanner, MediaProvider, MediaStore and other interfaces, and provides a set of database tables that are provided to users through Content Provider. When the mobile phone is turned on or if an SD card is plugged in or unplugged, the system will automatically scan the media files on the SD card and mobile phone memory, such as audio, video, pictures, etc., and place the corresponding information in the defined database table. In this program, we don’t need to care about how to scan files in our phones, just know how to query and use this information.
MediaStore defines a series of data tables. Through the query interface provided by ContentResolver, we can get various required information. Below we focus on querying music file information on the SD card.
Let’s first understand the query interface of ContentResolver:
Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder);
Uri: Indicate the database name to be queried plus the name of the table. We can find the parameters of the corresponding information from the MediaStore. For details, please refer to the development document.
Projection: Specify which columns in the query database table, and the returned cursor will include the corresponding information. Null returns all information.
selection: Specify query conditions
selectionArgs: Is there a parameter selection in the selection? This symbol is, and the actual value can be used instead of this question mark. If selection does not have this? If so, then this String array can be null.
SortOrder: Specify the order of the query results.
The following command will return information about all music files on the external memory card:
Cursor cursor = query(.EXTERNAL_CONTENT_URI, null, null, null, .DEFAULT_SORT_ORDER);
After obtaining the cursor, we can call the Cursor's related methods for specific music information:
Song ID:._ID
Int id = ((._ID));
The title of the song:
String tilte = (());
Album title of the song:
String album = (());
The name of the song:
String artist = (());
Full path to the song file:
String url = (());
The name of the song file: .DISPLAY_NAME
String display_name = ((.DISPLAY_NAME));
Release date of song file:
String year = (());
Total playback duration of the song:
Int duration = (());
Song file size:
Int size = (());