码迷,mamicode.com
首页 > 移动开发 > 详细

Android MediaScanner源代码解析

时间:2015-07-14 22:44:56      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:android   mediascann   media   

1. 简介

MediaScanner向上和MediaProvider.MediaScannerService交互, 响应其请求;
向下和JNI交互.
MediaScanner主要工作内容:
(1)接收MediaScannerService的scanDirectoriesscanSingleFile请求
(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

2. scanDirectories

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();
        }
    }

3. initialize

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;
        }
    }

4. prescan

void prescan(String filePath, boolean prescanFiles)
获取需要扫描文件列表,移除已删除文件信息

5. postscan

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)的工作是把信息写回数据库

6. processDirectory

调用native层

private native void processDirectory(String path, MediaScannerClient client);

版权声明:本文为博主原创文章,未经博主允许不得转载。

Android MediaScanner源代码解析

标签:android   mediascann   media   

原文地址:http://blog.csdn.net/matrix_laboratory/article/details/46744533

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!