The project may have such requirements: Download or import, export pictures, music and other media files, need to be able to display in the library or local video player immediately, or to be able to query the media database information about media files, then we have to proactively notify the system to scan the new media files. I have compiled a scan of the tool class, share under.
Specific code:
Public classMediascanner {Private volatile StaticMediascanner instance; PrivateMediascanner () {} Public StaticMediascanner Getinstace () {synchronized(Mediascanner.class){ if(Instance = =NULL) {instance=NewMediascanner (); } } returninstance; } /*** Scan a media file *@paramfilePath The media file to be scanned*/ Public void Scanfile (context context, Scanfile FilePath) {List<ScanFile> filepaths =NewArraylist<scanfile> (1); Filepaths.add (FilePath); Scanfiles (context, filepaths); } /*** Scan Multiple media files *@paramfilepaths List of files to scan*/ Public void Scanfiles (context context, list<scanfile> filepaths) {sannerclient Client=Newsannerclient (context, filepaths); Client.connectandscan (); } /*** Media file scan Object Builder*/ Public Static classscanfile{/*** Media files to be scanned path or folder paths containing media files*/ PublicString filepaths; /*** Media file type to scan Eg:audio/mp3 media/* Application/ogg * image/jpeg image/png video/mpeg VIDEO/3GPP * ...*/ PublicString Minetype; Publicscanfile (String filepaths, String minetype) { This. filepaths =filepaths; This. Minetype =Minetype; } } Public classSannerclientImplementsmediascannerconnection.mediascannerconnectionclient {/*** files or folders to scan*/ PrivateList<scanfile> Scanfiles =NULL; /*** A collection of individual files that are actually scanned*/ PrivateList<scanfile> filepaths =NULL; Privatemediascannerconnection Mediascanconn; PublicSannerclient (context context, list<scanfile>scanfiles) { This. Scanfiles =Scanfiles; Mediascanconn=NewMediascannerconnection (Context, This); } /*** Connect Mediascanner and start scanning*/ Public voidConnectandscan () {if(Scanfiles! =NULL&&!Scanfiles.isempty ()) { This. filepaths =NewArraylist<scanfile>(); //traversal gets a single collection of files for(Scanfile sf:scanfiles) {findfile (SF); } mediascanconn.connect (); } } Private voidfindfile (scanfile file) {file F=NewFile (file.filepaths); if(F.isfile ()) {filepaths.add (file); }Else{file[] fs=F.listfiles (); if(FS! =NULL&& fs.length > 0){ for(File cf:fs) {findfile (NewScanfile (Cf.getabsolutepath (), file.minetype)); } } } } Private voidScannext () {if(Filepaths! =NULL&&!Filepaths.isempty ()) {scanfile SF= Filepaths.remove (Filepaths.size ()-1); Mediascanconn.scanfile (Sf.filepaths, Sf.minetype); }Else{mediascanconn.disconnect (); }} @Override Public voidonmediascannerconnected () {scannext (); } @Override Public voidonscancompleted (String path, Uri uri) {//TODO auto-generated Method StubScannext (); //Media scan complete can be used with Eventbus and other message communication tools to send notifications, also can receive intent.action_media_scanner_finished broadcast//Eventbus.getdefault (). Post (new eventmediascancompleted (path)); } }}
Welcome feedback If there is a scan problem.
Android Media file scanning