Android Scan files or folders

Source: Internet
Author: User

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

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.