Android Media Scanner Process
[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 MediaFile.java. 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/vnd.ms-wpl");


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.

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Python 随机(random)模块的不可预测之美
  2. 如何构建可重复读取inputStream的request
  3. Python中的if...else语法和执行流程
  4. Java对象级别与类级别的同步锁synchroniz
  5. 老街华纳国际娱乐公司开户电话《17166918
  6. 【android】简单的根据url下载图片的一个
  7. Android获取statusBar和navigationBar高
  8. Android计时器和倒计时
  9. Android(安卓)关于显示键盘,布局错乱网上
  10. Android(安卓)代码片段---从相册或相机获