Android media files (images, videos, audios, and other files) must be scanned to the media library for viewing through related programs. For example
Write a program to download an image from the Internet and save it to the sdcard. Exit the program and open the system image program.
This is because the image is not registered in the system media library. When the system starts (or when sdcard is loaded again), the system scans the media file and registers the newly discovered file.
The following example shows how to register a new media file through code.
Import android. content. context; <br/> Import android. media. mediascannerconnection; <br/> Import android.net. uri; </P> <p> public class mediascanner {</P> <p> private mediascannerconnection mediascanconn = NULL; </P> <p> private musicsannerclient client = NULL; </P> <p> private string filepath = NULL; </P> <p> private string filetype = NULL; </P> <p> private string [] filepaths = NULL; <br/>/** <br/> * However Call mediascanner. scanfile ("/sdcard/2.mp3"); <br/> **/<br/> Public mediascanner (context) {<br/> // create a musicsannerclient <br/> If (client = NULL) {</P> <p> client = new musicsannerclient (); <br/>}</P> <p> If (mediascanconn = NULL) {</P> <p> mediascanconn = new mediascannerconnection (context, client ); <br/>}</P> <p> class musicsannerclient implements <br/> mediascannerconnection. m Ediascannerconnectionclient {</P> <p> Public void onmediascannerconnected () {</P> <p> If (filepath! = NULL) {</P> <p> mediascanconn. scanfile (filepath, filetype); <br/>}</P> <p> If (filepaths! = NULL) {</P> <p> for (string file: filepaths) {</P> <p> mediascanconn. scanfile (file, filetype); <br/>}</P> <p> filepath = NULL; </P> <p> filetype = NULL; </P> <p> filepaths = NULL; <br/>}</P> <p> Public void onscancompleted (string path, Uri URI) {<br/> // todo auto-generated method stub <br/> mediascanconn. disconnect (); <br/>}</P> <p>/** <br/> * scan file Tag Information <br/> * @ Param filepath file Path eg: /sdcard/mediaplayer/dahai.pdf <br/> * @ Param filetype file type eg: audio/MP3 media/* application/Ogg <br/> **/<br/> Public void scanfile (string filepath, string filetype) {</P> <p> This. filepath = filepath; </P> <p> This. filetype = filetype; <br/> // call the onmediascannerconnected () method of the musicsannerclient after the connection <br/> mediascanconn. connect (); <br/>}< br/>/** <br/> * @ Param filepaths file path <br/> * @ Param filetype file type <br/> **/< br/> Public void scanfile (string [] filepaths, string filetype) {</P> <p> This. filepaths = filepaths; </P> <p> This. filetype = filetype; </P> <p> mediascanconn. connect (); </P> <p >}</P> <p> Public String getfilepath () {</P> <p> return filepath; <br/>}</P> <p> Public void setfilepath (string filepath) {</P> <p> This. filepath = filepath; <br/>}</P> <p> Public String getfiletype () {</P> <p> return filetype; <br/>}</P> <p> Public void setfiletype (string filetype) {</P> <p> This. filetype = filetype; <br/>}</P> <p>}
Original post address: http://bashenmail.javaeye.com/blog/603680