In the default case. Media files that are created by your own programs and stored in the application's private folder are not available to other applications. These media files can be used by other applications. They need to be added to the Media Library, and there are two ways to do it now.
The first: Use a media scanner to add to the Media Library.
/*** Scan the files in the specified directory and add to the Media Library *@paramFilePath * Path*/ Private voidScanmedia (FinalString FilePath) {mediascannerconnectionclient Mediasan=Newmediascannerconnectionclient () {PrivateMediascannerconnection MSC =NULL; { //First you need to create a connection to the media scannerMSC =NewMediascannerconnection (mainactivity. This, This); Msc.connect (); } @Override Public voidonscancompleted (String path, Uri uri) {//The scan finishes closing the connection to the media scannerMsc.disconnect (); Mainactivity. This. Runonuithread (NewRunnable () {@Override Public voidrun () {Toast.maketext (mainactivity. This, "Scan done! ", Toast.length_short). Show (); } }); } @Override Public voidonmediascannerconnected () {//You can specify a MIME type if you do not specify media scaner to assume a type based on the file nameString MimeType =NULL; Msc.scanfile (FilePath, MimeType); } }; }
View Code
The main use here is "mediascannerconnectionclient" and "mediascannerconnection" two classes.
Implementation steps:
(1) Create a connection to the media scanner.
(2) Call the Scanfile method to scan the file.
(3) When the scan is complete, close the connection to the scanner.
Second: Manually add to Media Library
Contentvalues values =Newcontentvalues (); //title of the media fileValues.put (Audio.AudioColumns.TITLE, "My Media files"); //time StampValues.put (Audio.audiocolumns.date_added,system.currenttimemillis ()/1000); //File TypeValues.put (Audio.AudioColumns.MIME_TYPE, "Audio/amr"); //specifies the file path. Must be an absolute pathValues.put (MediaStore.Audio.Media.DATA, "/sdcard/myvideo.mp4"); //Insert the file into the Media Library ContentProviderContentresolver resolver =Getcontentresolver (); Uri URI=Resolver.insert (MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values); //send a broadcast. Notice that this media file is ready for use.Sendbroadcast (NewIntent (Intent.action_media_scanner_scan_file,uri));
View Code