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

Android MediaScanner.cpp 源代码解析

时间:2015-07-17 21:02:04      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:android   源代码   mediascan   

1. 简介

实现对文件夹的递归扫描
libmedia – libmedia.so
frameworks\av\media\libmedia\MediaScanner.cpp

2. 对文件的扫描

由JNI调用processDirectory,然后由doProcessDirectory和doProcessDirectoryEntry实现对文件夹的递归扫描。

2.1 doProcessDirectory

循环扫描该文件夹内的所有项

MediaScanResult MediaScanner::doProcessDirectory(
        char *path, int pathRemaining, MediaScannerClient &client, bool noMedia) {
    char* fileSpot = path + strlen(path);
    struct dirent* entry;
    .....
    // Treat all files as non-media in directories that contain a  ".nomedia" file
    .....
    //Skip .nomedia file

    DIR* dir = opendir(path);

    MediaScanResult result = MEDIA_SCAN_RESULT_OK;
    while ((entry = readdir(dir))) {
        if (doProcessDirectoryEntry(path, pathRemaining, client, noMedia, entry, fileSpot)
                == MEDIA_SCAN_RESULT_ERROR) {
            result = MEDIA_SCAN_RESULT_ERROR;
            break;
        }
    }
    closedir(dir);
    return result;
}

2.2 doProcessDirectoryEntry

判断该项目是文件、文件夹还是需要跳过的项。
if 文件夹
doProcessDirectory
else if 文件
通过JNI调用java层scanFile
else if skip
skip this file or folder

MediaScanResult MediaScanner::doProcessDirectoryEntry(
        char *path, int pathRemaining, MediaScannerClient &client, bool noMedia,
        struct dirent* entry, char* fileSpot) {
    struct stat statbuf;
    const char* name = entry->d_name;
    ........
    int type = entry->d_type;
    if (type == DT_UNKNOWN) {
        if (stat(path, &statbuf) == 0) {
            if (S_ISREG(statbuf.st_mode)) {
            //文件
                type = DT_REG;
            } else if (S_ISDIR(statbuf.st_mode)) {
            //文件夹
                type = DT_DIR;
            }
        }
    }
    if (type == DT_DIR) {
        bool childNoMedia = noMedia;
        // set noMedia flag on directories with a name that starts with ‘.‘
        // for example, the Mac ".Trashes" directory
        if (name[0] == ‘.‘)
            childNoMedia = true;

        // report the directory to the client
        if (stat(path, &statbuf) == 0) {
            status_t status = client.scanFile(path, statbuf.st_mtime, 0,
                    true /*isDirectory*/, childNoMedia);
            if (status) {
                return MEDIA_SCAN_RESULT_ERROR;
            }
        }

        // and now process its contents
        strcat(fileSpot, "/");
        MediaScanResult result = doProcessDirectory(path, pathRemaining - nameLength - 1,
                client, childNoMedia);
        if (result == MEDIA_SCAN_RESULT_ERROR) {
            return MEDIA_SCAN_RESULT_ERROR;
        }
    } else if (type == DT_REG) {
        stat(path, &statbuf);
        status_t status = client.scanFile(path, statbuf.st_mtime, statbuf.st_size,
                false /*isDirectory*/, noMedia);
        if (status) {
            return MEDIA_SCAN_RESULT_ERROR;
        }
    }

    return MEDIA_SCAN_RESULT_OK;
}

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

Android MediaScanner.cpp 源代码解析

标签:android   源代码   mediascan   

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

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