How to save downloaded multimedia files to the system media library in Android

Source: Internet
Author: User

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

Related Article

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.