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

更多相关文章

  1. Android读取Assert文件夹下txt文本并变为String的方法
  2. (Android(安卓)studio)关于drawable文件夹的问题
  3. android ndkR7以上的环境配置搭建(wondows及linux)
  4. Android(安卓)UI【Android(安卓)仿Iphone文件夹分裂效果】
  5. Android(安卓)定制EditText 改变 底线颜色
  6. Android(安卓)二维码扫描源码下载
  7. Android真机测试,Find Explorer无法打开data文件夹解决办法
  8. Android真机连接Eclipse时,打不开File Explorer下的data文件夹解
  9. Android(安卓)仿Iphone文件夹分裂效果

随机推荐

  1. SQL Server导入导出数据时最常见的一个错
  2. SQL Server中使用sp_password重置SA密码
  3. SQL Server数据库bcp导出备份文件应用示
  4. Sql Server使用cursor处理重复数据过程详
  5. SqlServer实现类似Oracle的before触发器
  6. 在Windows XP系统安装SQL server 2000 企
  7. 一个统计表每天的新增行数及新增存储空间
  8. win7系统安装SQLServer2000的详细步骤(图
  9. sql server判断数据库、表、列、视图是否
  10. SQL中varchar和nvarchar的基本介绍及其区