Before the 4.4 release, we can force the Media Library to be refreshed by sending a broadcast
Intentfilter Intentfilter =New Intentfilter ( intent.action_media_scanner_started); Intentfilter.addaction (intent.action_media_scanner_finished); Intentfilter.adddatascheme ( "file"); Sendbroadcast (new Intent (intent.action_media_mounted, Uri.parse ( "file://" + environment.getexternalstoragedirectory ()
After you send the broadcast, you also need to register a broadcast recipient to accept and process the scan start and end events
PrivateClassScanreceiverExtendsBroadcastreceiver { Publicvoid OnReceive (Context context, Intent Intent) { //when the system starts scanning the SD card, for the user experience, You can add a wait box if (intent.action _media_scanner_started.equals (Action)) { } //when the system scan is complete, stop the display of the Wait box, and requery Contentprovider if ( Intent.ACTION_MEDIA_SCANNER_FINISHED.equals (ACTION)) { } }} /span>
In this way, we can force the Media Library to be updated.
However, after 4.4, Android has increased permissions on some operations, and if you use this method on a 4.4 system, the following error will occur
From pid=22360, uid=10163
This is because after 4.4, this broadcast can only be issued by the system application, so we can't use this method, we could use the following code to achieve the same function:
Mediascannerconnection.scanfile (new string[] {environment null);
Using Mediascannerconnection's Scanfile method, we can force scan the file path we need to update, and then the media database will be updated synchronously, so that there will be no file deletion, in the Media Library can search for the situation, can also solve the new song that the friend raised the information acquisition problem.
Original:
Http://www.tuicool.com/articles/vyYZny
How to get all the audio file information in the library and update the Media Library when the file is deleted