1. Introduction
Implementing recursive scanning of folders
Libmedia–libmedia.so
Frameworks\av\media\libmedia\mediascanner.cpp
2. Scanning of files
The processdirectory is called by JNI, and then the recursive scan of the folder is implemented by Doprocessdirectory and Doprocessdirectoryentry.
2.1 Doprocessdirectory
Cycle through all items in the folder
Mediascanresult Mediascanner::d oprocessdirectory (Char*path,intPathremaining, Mediascannerclient &client,BOOLNoMedia) {Char* Filespot = path + strlen (path);structdirent* 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);returnResult;}
2.2 Doprocessdirectoryentry
determines whether the item is a file, a folder, or an item that needs to be skipped.
If folder
Doprocessdirectory
else if file
Calling Java layer Scanfile via JNI
else if Skip
Skip this file or folder
Mediascanresult Mediascanner::d oprocessdirectoryentry (Char*path,intPathremaining, Mediascannerclient &client,BOOLNoMedia,structdirent* entry,Char* Filespot) {structStat 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)) {//Documents type= Dt_reg; }Else if(S_isdir (Statbuf.st_mode)) {//Folder type= Dt_dir; } } }if(type= = Dt_dir) {BOOLChildnomedia = NoMedia;//Set NoMedia flag on directories with a name the starts with '. ' //For example, the MAC. Trashes "Directory if(name[0] =='. ') Childnomedia =true;// Report of 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) {returnMedia_scan_result_error; } }//And now process its contentsstrcat (Filespot,"/"); Mediascanresult result = doprocessdirectory (path, Pathremaining-namelength-1, client, Childnomedia);if(Result = = Media_scan_result_error) {returnMedia_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) {returnMedia_scan_result_error; } }returnMEDIA_SCAN_RESULT_OK;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android MediaScanner.cpp Source Code parsing