Android MediaScanner.cpp 源代码解析

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 fileDIR* 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;(name[0] == ‘.’)childNoMedia = true;// report the directory to the clientif (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 contentsstrcat(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 源代码解析

相关文章:

  • 【算法】直接插入排序C语言实现
  • 嵌入式 FAAC1.28 在海思HI3518C/HI3518A平台linux中的编译优化
  • Android 动画animation 深入分析
  • Mybatis极其(最)简(好)单(用)的一个分页插件
  • Ext JS Kitchen Sink [Learning by doing](2)ArrayGrid
  • API开发第三篇:PHP的设计模式之完美的单例模式
  • 你感兴趣的文章:

    标签云:

    亚洲高清电影在线, 免费高清电影, 八戒影院夜间, 八戒电影最新大片, 出轨在线电影, 午夜电影院, 在线影院a1166, 在线电影院, 在线观看美剧下载, 日本爱情电影, 日韩高清电影在线, 电影天堂网, 直播盒子app, 聚合直播, 高清美剧, 高清美剧在线观看 EhViewer-E站, E站, E站绿色版, qqmulu.com, qq目录网, qq网站目录,