MediaScanner向上和MediaProvider.MediaScannerService交互, 响应其请求;
向下和JNI交互.
MediaScanner主要工作内容:
(1)接收MediaScannerService的scanDirectories和scanSingleFile请求
(2)获取各个ContentProvider 环境(Audio,Video,Image,File) initialize
(3)获取需要扫描文件列表,移除已删除文件信息 prescan
(4)通过media_jni(android_media_MediaScanner.cpp)调用底层接口处理扫描
(5)接收media_jni处理结果,在android_media_MediaScanner.cpp中保存一个MyMediaScannerClient用于调用MediaScanner
(6)保存获取的文件信息 postscan
代码位置:
frameworks/base/media/java/android/media/MediaScanner.java
scanDirectories(String[] directories, String volumeName)
扫描文件夹,被MSS调用
MediaScanner处理文件夹的具体流程是:
在MediaScanner中
核心代码如下:
public void scanDirectories(String[] directories, String volumeName) {
......
initialize(volumeName);
prescan(null, true);
......
for (int i = 0; i < directories.length; i++) {
processDirectory(directories[i], mClient);
}
......
postscan(directories);
......
releaseResources();
}
}
void initialize(String volumeName)
initialize和releaseResources相对应
功能就是初始化各个ContentProvider (Audio,Video,Image,File)
private void initialize(String volumeName) {
mMediaProvider = mContext.getContentResolver().acquireProvider("media");
mAudioUri = Audio.Media.getContentUri(volumeName);
mVideoUri = Video.Media.getContentUri(volumeName);
mImagesUri = Images.Media.getContentUri(volumeName);
mThumbsUri = Images.Thumbnails.getContentUri(volumeName);
mFilesUri = Files.getContentUri(volumeName);
mFilesUriNoNotify = mFilesUri.buildUpon().appendQueryParameter("nonotify", "1").build();
if (!volumeName.equals("internal")) {
// we only support playlists on external media
mProcessPlaylists = true;
mProcessGenres = true;
mPlaylistsUri = Playlists.getContentUri(volumeName);
mCaseInsensitivePaths = true;
}
}
void prescan(String filePath, boolean prescanFiles)
获取需要扫描文件列表,移除已删除文件信息
void postscan(String[] directories)
把获取的文件信息写回数据库
其主要工作在
void processPlayLists()
和
void processPlayList(FileEntry entry, Cursor fileList)
完成
5.1 processPlayLists()
private void processPlayLists() throws RemoteException {
Iterator<FileEntry> iterator = mPlayLists.iterator();
Cursor fileList = null;
try {
// use the files uri and projection because we need the format column,
// but restrict the query to just audio files
fileList = mMediaProvider.query(mPackageName, mFilesUri, FILES_PRESCAN_PROJECTION,
"media_type=2", null, null, null);
while (iterator.hasNext()) {
FileEntry entry = iterator.next();
// only process playlist files if they are new or have been modified since the last scan
if (entry.mLastModifiedChanged) {
processPlayList(entry, fileList);
}
}
}
}
5.2 processPlayList(FileEntry entry, Cursor fileList)
processPlayList(FileEntry entry, Cursor fileList)的工作是把信息写回数据库
调用native层
private native void processDirectory(String path, MediaScannerClient client);
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/matrix_laboratory/article/details/46744533