We may often encounter this situation, obviously saved the picture, but when you open the picture, but did not find the picture, the phone restarts before you can see. This is because the SD card is not re-mounted, the library can not be loaded into the picture, to solve the problem is very simple, we only need to simulate a broadcast, so that the SD card can be mounted again!
public static void Sendbroadcaseremountsdcard (Activity activity,string filepath) {
try{
if (filepath==null) {
if (Build.VERSION.SDK_INT >= build.version_codes. KITKAT) {
Intent mediascanintent = new Intent (
Intent.action_media_scanner_scan_file);
Uri Contenturi = Uri.fromfile (Environment.getexternalstoragedirectory ()); Specify the SD card path
Mediascanintent.setdata (Contenturi);
Activity.sendbroadcast (mediascanintent);
}
else {
Activity.sendbroadcast (New Intent (
Intent.action_media_mounted,
Uri.parse ("file://" + environment.getexternalstoragedirectory ()));
}
}
else{
Uri data = Uri.parse ("file://" +filepath);
Activity.sendbroadcast (New Intent (Intent.action_media_scanner_scan_file, data));
}
}
catch (Exception ex) {
Logutil.writeerror (ex);
LOG.E ("Sendbroadcaseremountsdcard", Ex.getmessage ());
Toastutil.show (Activity, "re-mount SD card failed, please restart your phone to view pictures");
}
}
This is useless. To reload, you must scan the file, to re-mount the folder, and to recursively scan all folders under the file.
The implementation code is as follows:
//Scan Files Public Static voidfilescan (Activity activity,string file) {Uri data= Uri.parse ("file://"+file); Activity.sendbroadcast (NewIntent (intent.action_media_scanner_scan_file, data)); } //Scan Folders Public Static voidFolderscan (Activity activity,string path) {File file=NewFile (path); if(File.isdirectory ()) {file[] array=File.listfiles (); for(intI=0; i<array.length;i++) {File F=Array[i]; if(F.isfile ()) {//FILE TYPEString name =F.getname (); if(Name.contains (". mp3") {FileScan (Activity,f.getabsolutepath ()); } } Else{//FOLDER TYPEFolderscan (Activity,f.getabsolutepath ()); } } } }
Android Scan files or folders