Android Media file scanning

Source: Internet
Author: User

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

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.