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.
分享到:
相关推荐
Android Media Scanner Process则涉及媒体文件的扫描和管理。从JAVA层初始化、预扫描到C++层处理目录,再到JAVA层扫描文件和后扫描,这个过程解释了设备如何识别并组织媒体内容。 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源码解析与应用》 在移动设备领域,条形码扫描技术已经成为一个不可或缺的功能,尤其在商品管理、物联网应用以及信息快速获取等方面发挥着重要作用。Android平台上的BarcodeScanner应用...
《Android BarcodeScanner3详解》 在移动开发领域,Android平台上的条形码扫描应用是不可或缺的一部分,其中 BarcodeScanner3 是一款高效且实用的工具。它允许用户通过手机摄像头捕获并解析各种类型的条形码,如QR...
在Android的原生多媒体框架中,mediaScanner的JNI实现通常位于`frameworks/base/media/jni/`目录下,文件名为`android_media_MediaScanner.cpp`。这里定义了native方法的实现,负责将Java层的扫描任务委托给底层的...
综上所述,`android_media_MediaScanner.rar_android_mediascanner`涉及的知识点广泛且深入,包括文件系统监控、媒体信息解析、SQLite操作、异常处理、性能优化、权限管理和服务组件等,这些都是Android系统中不可或...
'android.intent.action.MEDIA_SCANNER_FINISHED' 开始扫描介质的一个目录。 'android.intent.action.MEDIA_SCANNER_STARTED' 扩展介质的挂载被解除 (unmount)。 因为它已经作为 USB 大容量存储被共享。 'android....
Scanner Compat库解决了在Android上扫描Bluetooth Low Energy设备的问题。 最初在Android 4.3中创建的扫描仪API在Android 5.0中已更改,并在6.0和8.0中进行了扩展。 该库甚至可以在较旧的手机上使用现代API,模拟不...
"东集android开发包_scanner"是一个专为开发者设计的工具包,它包含了东集手持设备Android端扫描功能的相关组件和示例代码,帮助开发者理解和实现条形码、二维码等扫描功能。 这个开发包的核心部分是ScannerAPI,它...
《安卓Android源码——BarcodeScanner深度解析》 在Android开发领域,掌握源码分析是提升技术能力的关键步骤。本文将深入探讨"BarcodeScanner"这一应用的源码,它是一款用于扫描和识别条形码和二维码的工具。通过...
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 ...
2. **Broadcast Intent (ACTION_MEDIA_SCANNER_SCAN_FILE)**: 发送一个广播Intent,指定ACTION_MEDIA_SCANNER_SCAN_FILE动作,这样系统媒体扫描器会扫描指定文件并将其加入数据库。 3. **利用...
Android多媒体扫描框架,即Media Scanner,是Android系统中用于检测和处理媒体文件(如音频、视频和图片)的重要组件。当设备启动、媒体设备挂载或接收到特定广播意图时,MediaScannerReceiver会被触发,进而启动...
端口扫描forAndroid 端口扫描forAndroid 端口扫描forAndroid 端口扫描forAndroid 端口扫描forAndroid
AndroidQRCodeScanner:Android QR Code Scanner是一个用Java编写的Android应用程序,它使用Android Jetpack CameraX库与ZXing开源条形码扫描库一起预览相机和图像分析
"安卓串口Socket通讯USB驱动jni相关-AndroidScanner扫描目标设备可用端口的小程序.rar"是一个针对这一需求的解决方案。 首先,让我们理解“串口Socket通讯”。串口通信是一种古老的接口标准,广泛用于设备间的通信...
基于barcodescanner实现Android二维码扫描功能 Android二维码扫描功能是当前移动应用程序中非常常见的一种基础功能。随着二维码技术的普及,二维码扫描功能已经成为许多应用程序的必备功能之一。在Android平台上...