Android MediaScanner 总纲

Posted wzjhoutai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android MediaScanner 总纲相关的知识,希望对你有一定的参考价值。

1. MediaScanner HEAD

技术分享

2. 应用层 MediaProvider

packages\providers\MediaProvider
(1) MediaProvider
packages\providers\MediaProvider\MediaProvider.java
MediaProvider继承于ContentProvider,主要用于创建媒体库的数据库表.
具体内容请查阅怎样创建Provider.
MediaProvider中包涵一个BroadcastReceiver,用于监听SDCard挂载/卸载
(2) MediaReceiver
监听扫描事件, 调用MSS
Action :
Intent.ACTION_BOOT_COMPLETED, Intent.ACTION_MEDIA_MOUNTED
scan volume MediaScannerService
else
scanFile filepath MediaScannerService
(3) MediaScannerService MSS
onStartCommand==>ServiceHandler==>scanFile
当我们调用startService的时候。假设相应的Service还未创建就会调用onCreate方 法===方法。

每次startService的时候就调用onStartCommand,所以ServiceHandler就在此发送消息了。

call: MediaScanner :: scanSingleFile, scanDirectories

3. JAVA 层

Call Stack : Google注解

/*
 * In summary:
 * Java MediaScannerService calls
 * Java MediaScanner scanDirectories, which calls
 * Java MediaScanner processDirectory (native method), which calls
 * native MediaScanner processDirectory, which calls
 * native MyMediaScannerClient scanFile, which calls
 * Java MyMediaScannerClient scanFile, which calls
 * Java MediaScannerClient doScanFile, which calls
 * Java MediaScanner processFile (native method), which calls
 * native MediaScanner processFile, which calls
 * native parseMP3, parseMP4, parseMidi, parseOgg or parseWMA, which calls
 * native MyMediaScanner handleStringTag, which calls
 * Java MyMediaScanner handleStringTag.
 * Once MediaScanner processFile returns, an entry is inserted in to the database.
 */

技术分享
(1) MediaScanner
frameworks/base/media/java/android/media/MediaScanner.java

Call JNI

    static {
        System.loadLibrary("media_jni");
        native_init();
    }

scanDirectories ? jni processDirectory
scanSingleFile ==> MyMediaScannerClient.doScanFile
deal with image, audio, video ? jni processFile

4. JNI层

(1) android_media_MediaScanner.cpp
frameworks\base\media\jni\android_media_MediaScanner.cpp
MediaScanner *mp = getNativeScanner_l(env, thiz);
mp->processDirectory(pathStr, myClient);
mp->processFile(pathStr, mimeTypeStr, myClient);

android_media_MediaScanner中比較重要的是MyMediaScannerClient类, 该类会被继续传究竟层,用于调用java层(MediaScanner.java)的 scanFile, handleStringTag, setMimeType

5. Server Proxyer

libmedia – libmedia.so
frameworks\av\media\libmedia\MediaScanner.cpp
处理具体的文件
假设是目录须要返回JAVA层中,採用scanFile处理

6. Server

frameworks\av\media\libstagefright\StagefrightMediaScanner.cpp
libstagefright.so
依据文件后缀推断可处理的文件
然后通过MediaMetadataRetriever获取视频的具体信息,
最后同过handleString一步一步向上传, 在MediaScannerService中把信息写入数据库.































以上是关于Android MediaScanner 总纲的主要内容,如果未能解决你的问题,请参考以下文章

[深入理解Android卷一全文-第十章]深入理解MediaScanner

深入理解JNI

转自:Android MediaScanner 详尽分析

Android多媒体分析MediaScanner

Android如何通知MediaScanner扫描出指定文件

Android面试复习总纲