The following figure shows the system.
Mediascannerpolicer will be in any action_boot_completed,
Start when action_media_mounted or action_media_scanner_scan_file intent (intent) is sent. Because parsing the metadata of a media file may take a long time, mediascannerpolicer starts mediascannerservice.
Mediascannerservice calls a public class mediascanner to handle real work. Mediascannerreceiver maintains two types of scan directories: one is the internal volume (internal
Volume) points to $ (android_root)/media. The other is external volume (external
Volume) points to $ (external_storage ).
The scanning and parsing work is at the Java and C ++ layers. The Java layer is the initiator. To scan all directories, follow these steps:
1. Java layer Initialization
In this step, it opens different databases based on whether the directory is an internal volume or an external volume.
2. Java layer pre-Scan
First, clear cache entries for files and playlists. Then, a new file and a playlist cache entry are generated based on the request results returned by mediaprovider.
3. c ++ layer processing Directory
List all files and specific subdirectories (if the subdirectory contains a. nomedia hidden file, it will not be listed .). The listed file is used to determine whether the file is supported based on the file extension. If this file extension is supported, the C ++ layer calls back to the Java layer to scan the file. This extension will be scanned and listed in mediafile. java. The following is a list of supported file extensions.
/* Audio */
Addfiletype ("MP3", file_type_mp3, "audio/MPEG ");
Addfiletype ("m4a", file_type_m4a, "audio/MP4 ");
Addfiletype ("WAV ",
File_type_wav, "audio/X-WAV ");
Addfiletype ("Amr", file_type_amr, "audio/AMR ");
Addfiletype ("AWB", file_type_awb, "audio/AMR-WB ");
Addfiletype ("WMA ",
File_type_wma, "audio/X-MS-WMA ");
Addfiletype ("Ogg", file_type_ogg, "application/Ogg ");
Addfiletype ("mid ",
File_type_mid, "audio/Midi ");
Addfiletype ("xmf", file_type_mid, "audio/Midi ");
Addfiletype ("rtttl", file_type_mid, "audio/Midi ");
Addfiletype ("SMF ",
File_type_smf, "audio/SP-Midi ");
Addfiletype ("IMy", file_type_imy, "audio/imelody ");
/* Video */
Addfiletype ("MP4", file_type_mp4, "Video/MP4 ");
Addfiletype ("m4v ",
File_type_m4v, "Video/MP4 ");
Addfiletype ("3GP", file_type_3gpp, "Video/3GPP ");
Addfiletype ("3GPP", file_type_3gpp, "Video/3GPP ");
Addfiletype ("3g2 ",
File_type_3gpp2, "Video/3gpp2 ");
Addfiletype ("3gpp2", file_type_3gpp2, "Video/3gpp2 ");
Addfiletype ("WMV ",
File_type_wmv, "Video/X-MS-WMV ");
/* Image */
Addfiletype ("jpg ",
File_type_jpeg, "image/JPEG ");
Addfiletype ("Jpeg", file_type_jpeg, "image/JPEG ");
Addfiletype ("GIF ",
File_type_gif, "image/GIF ");
Addfiletype ("PNG", file_type_png, "image/PNG ");
Addfiletype ("BMP", file_type_bmp,
"Image/X-MS-BMP ");
Addfiletype ("wbmp", file_type_wbmp, "image/vnd. WAP. wbmp ");
/* Audio play list */
Addfiletype ("M3U", file_type_m3u,
"Audio/X-mpegurl ");
Addfiletype ("pls", file_type_pls, "audio/X-scpls ");
Addfiletype ("WPL ",
File_type_wpl, "application/vnd. MS-WPL ");
4. Scan files at the Java Layer
A) Java-layer start File
First, it ignores some MACOs
And Windows Media
Player special files. Then, it checks whether the scanned file already exists in the cache entry. If yes, it checks whether the last modification time of the file has changed. Finally, it returns whether the file needs further processing. If you do not need it, the next two steps will not be executed.
B) Scanning files at the C ++ Layer
Not all files must be handed over to the C ++ layer for parsing as metadata. Only the following file types will be parsed. Note that image files are not processed here.
- If (mfiletype = mediafile. file_type_mp3 |
- Mfiletype = mediafile. file_type_mp4 |
- Mfiletype = mediafile. file_type_m4a |
- Mfiletype = mediafile. file_type_3gpp |
- Mfiletype = mediafile. file_type_3gpp2 |
- Mfiletype = mediafile. file_type_ogg |
- Mfiletype = mediafile. file_type_mid |
- Mfiletype = mediafile. file_type_wma ){
- ......
- }
Copy code
The C ++ layer calls back the parsed metadata to the handlestringtag of the Java layer. The Java layer records its name/value information.
C) End file of the Java Layer
Based on the value parsed in the previous step, the Java layer updates the database tables generated by the corresponding meidaprovider.
5. Java layer send Scan
So far, all files have been scanned, And it will finally check the cached entries in the file and playlist to see if all items still exist in the file system. If an entry is empty, it is deleted from the database. In this way, it can maintain consistency between the database and the file system.
Other applications receive the action_media_scanner_started message from the mediascannerservice.
And the action_media_scanner_finished intent can be used to know when the scan operation starts and ends.