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.
分享到:
相关推荐
5 Android Media Scanner Process 27 5.1 JAVA layer initialize 27 5.2 JAVA layer prescan 28 5.3 C++ layer processDirectory 28 5.4 JAVA layer scanFile 29 5.5 JAVA layer postScan 30 6 Android Mount Daemon...
Android Media Scanner Process.pdf Talking about Android process.pdf Android Initialization Process.pdf surfaceflinger.pdf Android Build System by google.pdf auug97.pdf Android 图形系统.pdf 如何在...
Android-Scanner-Compat-Library, 在Android上,用于蓝牙智能扫描的兼容 多功能扫描仪兼容程序库 扫描仪兼容的库通过在Android上扫描蓝牙智能设备解决了这个问题。 scanner在 Android 5.0中已经改变,并在 6.0中扩展...
android BarcodeScanner源码.zip
\BarcodeScanner3
该文是对Android_MediaScanner 的详尽分析,欢迎大家下载学习
Scanner Compat库解决了在Android上扫描Bluetooth Low Energy设备的问题。 最初在Android 4.3中创建的扫描仪API在Android 5.0中已更改,并在6.0和8.0中进行了扩展。 该库甚至可以在较旧的手机上使用现代API,模拟不...
安卓Android源码——BarcodeScanner.zip
Android平台OCR识别的源代码,需要的可以借鉴参考。
Privacy Friendly QR Scanner The Privacy Friendly QR Scanner belongs to the group of Privacy Friendly Apps. The app supports its users in detecting malicious links: QR Codes provide new possibilities ...
东集seuic开发andriod工具包scanner版,是东集手持设备andriod端扫描功能的示例文档,可以用来练习相关软件的使用。
'android.intent.action.MEDIA_SCANNER_FINISHED' 开始扫描介质的一个目录。 'android.intent.action.MEDIA_SCANNER_STARTED' 扩展介质的挂载被解除 (unmount)。 因为它已经作为 USB 大容量存储被共享。 'android....
端口扫描forAndroid 端口扫描forAndroid 端口扫描forAndroid 端口扫描forAndroid 端口扫描forAndroid
BarcodeScanner安卓二维码扫描源码
AndroidQRCodeScanner:Android QR Code Scanner是一个用Java编写的Android应用程序,它使用Android Jetpack CameraX库与ZXing开源条形码扫描库一起预览相机和图像分析
FIND3 Android App This is the open-source minimal Android app for using with FIND3. This app will allow you to use your phone to perform constant scans of Bluetooth and WiFi signals and levels that ...
Android Scanner扫描目标设备可用端口的小程序.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
Code-Scanner for Android v1.0 声明 本专案资料来源、Source Code引用来自: LivotovLabs/zxscanlib( ) 专案贡献: (1) 提升扫描器效能:降低扫描器执行Decode的频率以提升效能。由于原本的元件在扫描时,扫描器在...
媒体扫描仪扫描存储设备中的媒体文件