Android Mediascanner Source Code parsing

Source: Internet
Author: User

1. Introduction

Mediascanner up and Mediaprovider.mediascannerservice to respond to their requests;
Down and JNI interaction.
Mediascanner main work content:
(1) Receiving Mediascannerservice's scandirectories and scansinglefile requests
(2) Get each ContentProvider environment (audio,video,image,file) Initialize
(3) Get a list of files to be scanned and remove deleted file information Prescan
(4) Call the underlying interface to process the scan via Media_jni (android_media_mediascanner.cpp)
(5) Receive MEDIA_JNI processing results, save a mymediascannerclient in Android_media_mediascanner.cpp to invoke Mediascanner
(6) Save the obtained file information Postscan
Code Location:
Frameworks/base/media/java/android/media/mediascanner.java

2. Scandirectories

Scandirectories (string[] directories, String volumename)
Scan folder, called by MSS
The specific process for Mediascanner processing folders is:
In the Mediascanner
The core code is as follows:

    publicvoidscanDirectories(String[] directories, String volumeName) {            ......            initialize(volumeName);            prescan(nulltrue);            ......            for (int0; i < directories.length; i++) {                processDirectory(directories[i], mClient);            }            ......            postscan(directories);             ......            releaseResources();        }    }
3. Initialize

void Initialize (String volumename)
Initialize and releaseresources correspond
function is to initialize each contentprovider (audio,video,image,file)

private void Initialize (String volumename) {Mmediaprovider = Mcontext. Getcontentresolver(). Acquireprovider("Media");Maudiouri = Audio. Media. Getcontenturi(VolumeName);Mvideouri = Video. Media. Getcontenturi(VolumeName);Mimagesuri = Images. Media. Getcontenturi(VolumeName);Mthumbsuri = Images. Thumbnails. Getcontenturi(VolumeName);Mfilesuri = Files. Getcontenturi(VolumeName);Mfilesurinonotify = Mfilesuri. Buildupon(). Appendqueryparameter("Nonotify","1"). Build();if (!volumename. Equals("Internal") {//We only support playlists on external media mprocessplaylists = True;Mprocessgenres = True;Mplaylistsuri = Playlists. Getcontenturi(VolumeName);Mcaseinsensitivepaths = True;}    }
4. Prescan

void Prescan (String filePath, Boolean prescanfiles)
Get a list of files that need to be scanned to remove deleted file information

5. Postscan

void Postscan (string[] directories)
Write the retrieved file information back to the database
Its main work in
void Processplaylists ()
And
void Processplaylist (Fileentry entry, Cursor fileList)
Complete

5.1 processplaylists ()

    Private void processplaylists()throwsremoteexception {iterator<fileentry> Iterator = Mplaylists.iterator (); Cursor fileList =NULL;Try{//Use the files Uri and projection because we need the format column,            //But restrict the query to just audio filesFileList = Mmediaprovider.query (Mpackagename, Mfilesuri, Files_prescan_projection,"media_type=2",NULL,NULL,NULL); while(Iterator.hasnext ()) {Fileentry entry = Iterator.next ();//Only process playlist files if they is new or has been modified since the last scan                if(entry.mlastmodifiedchanged) {processplaylist (entry, fileList); }            }        }    }

5.2 Processplaylist (fileentry entry, Cursor fileList)
The job of processplaylist (Fileentry entry, Cursor fileList) is to write the information back to the database

6. Processdirectory

Call native Layer

privatenativevoidprocessDirectory(String path, MediaScannerClient client);

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Mediascanner 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.