Android MediaScanner.cpp Source Code parsing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.