android 多媒体扫描过程(Android Media Scanner Process)

原创 2011年01月07日 19:17:00




[First written by Steve Guo, please keep the mark if forwarding.] 

Here is the overall picture.

MediaScannerReceiver is started when any ACTION_BOOT_COMPLETED, ACTION_MEDIA_MOUNTED or ACTION_MEDIA_SCANNER_SCAN_FILE intent is sent out. Because it may cost a lot of time to parse meta-data information of media files, MediaScannerReceiver will start MediaScannerService. MediaScannerService calls a utility class MediaScanner to do the real work. MediaScannerReceiver maintains two kinds of scan directories: One is internal volume which points to $(ANDROID_ROOT)/media. Another is external volume which points to $(EXTERNAL_STORAGE).

The scan and parse work lies in both JAVA and C++ layer. JAVA layer is the starter. To scan the whole directory, MediaScanner does the following steps.

1. JAVA layer initialize

In this step, it will open different database according to whether the dir is internal or external volume.

2. JAVA layer prescan

It first clears file and play list cache entries, then generates new file and play list cache entries according to query result from MediaProvider.

3. C++ layer processDirectory

It enumerates all files and sub-dirs in a specific dir(If a sub-dir contains a .nomedia hidden file, it won’t be enumerated.). For each enumerated file, it judges whether the file is supported according to file extension. If the file extension is supported, then C++ layer will call back to JAVA layer scanFile. The file extension which will be scanned is listed in Here is the list.

/* 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/");

4. JAVA layer scanFile

a) JAVA layer beginFile

First it ignores some special files for MacOS and Windows Media Player. Then it looks whether the file has been in the cache entry, if so, it will check whether the file’s last modification time is changed. Finally it returns the result whether the file needs to be processed further. If no need, the following two steps won’t be executed.

b) C++ layer scanFile

Not all the files will be delivered to let C++ layer parse meta-data. Only the following file types will be parsed. Pay attention here, image files are not handled 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) {

For each parsed meta-data information, C++ layer will call back to JAVA layer handleStringTag. JAVA layer will record the name/value information.

c) JAVA layer endFile

Finally JAVA layer updates the corresponding database table provided by MeidaProvider according to the values parsed at the previous step.

5. JAVA layer postScan

Until now, all the files have been scanned, it finally checks the file and play list cache entry to see whether all items still existed in the file system. If have any empty entry, then delete it from database. So that it can keep some kinds of consistence between database and file system.

Other application can know when the scan operation starts and ends through receiving ACTION_MEDIA_SCANNER_STARTED and ACTION_MEDIA_SCANNER_FINISHED intents sent from MediaScannerService.


MediaScanner主要功能:把上层修改写入到数据库。在android中使用轻量级数据库sqlite来存储数据,使用sql语句可查看数据库中的内容。 1. 扫描文件 在对文件进行重命名、删...
  • minmin1027
  • minmin1027
  • 2015年11月25日 10:16
  • 1332


  • u010164190
  • u010164190
  • 2017年01月17日 22:08
  • 930

android 媒体数据库刷新

  • Joyce_Qin_1986
  • Joyce_Qin_1986
  • 2013年04月09日 15:46
  • 4335


想发起media扫描一般我们有两种方式: 1.发送广播 我们先不讲原理,先来看看Android源码中是如何发起一次扫描的 private void updateMediaStore(String...
  • zx64881926
  • zx64881926
  • 2017年06月05日 21:13
  • 547


  • w6980112
  • w6980112
  • 2016年02月01日 17:18
  • 1853


代码: 依赖包模式 基于zxing.jar库升级封装成傻瓜模式 二维码启动...
  • huangbin95487710
  • huangbin95487710
  • 2015年11月11日 13:09
  • 2464

修改Android Media Scanner的扫描路径

转载自: 对Android稍有熟悉的人都知道,Android Media Sca...
  • tongfei8591
  • tongfei8591
  • 2014年03月05日 15:08
  • 414


  • itluochen
  • itluochen
  • 2016年08月06日 00:09
  • 1444


MediaPlayer的唤醒锁   一般使用MediaPlayer播放音频流,推荐使用一个Service来承载MediaPlayer,而不是直接在Activity里使用。但是Android系统的功...
  • duanyu218
  • duanyu218
  • 2014年06月10日 11:02
  • 1311


Android多媒体涉及视频、音频、图片,首先有几个概念: 1.Android中所有的多媒体文件除了存储在物理硬盘上,还会将文件信息(路径、id、类型、)保存到数据库中,这样当我们想要获取这些文件的...
  • c12366456
  • c12366456
  • 2016年05月15日 23:07
  • 1121
您举报文章:android 多媒体扫描过程(Android Media Scanner Process)