When you get to this article, you must have encountered the problem of not finding the latest photos when you need to use a local album in the Android4.4 version. Before Android4.4 in the need to obtain the phone local picture information, only need to send a broadcast update album information, but the system updated to 4.4 after the broadcast is invalid and error.
The broadcast is as follows:
Sendbroadcast (New Intent (intent.action_media_mounted, Uri.parse ("file://" + Environment.getexternalstoragedirectory ())));
The broadcast plays a role in updating the scanned media file, and then 4.4 withdraws the permission. To do Android development, due to version compatibility, developed products need to test various versions of the system, testing the cost is greater. All right, here's how to do this.
1. Get the System Album data method
1Uri Imageuri =MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 2Contentresolver Mcontentresolver =Getapplicationcontext (). Getcontentresolver (); 3 //only images of JPEG and PNG are queried4Cursor mcursor = Mcontentresolver.query (Imageuri,NULL, MediaStore.Images.Media.MIME_TYPE +"=? or"+ MediaStore.Images.Media.MIME_TYPE +"=?", 5 NewString[] {"Image/jpeg","Image/png"}, mediastore.images.media.date_taken+" "+"desc"); 6 while(Mcursor.movetonext ()) {7 //get the path to a picture8String Path =mcursor.getstring (Mcursor.getcolumnindex (MediaStore.Images.Media.DATA));9 if(!NewFile (Path). Isfile ())Continue;TenLOG.I ("Tag", path); One } AMcursor.close ();
Permissions:
1 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />2 <uses-permission android:name="Android.permission.READ_EXTERNAL_STORAGE "/>3 <uses-permission android:name="android.permission.MOUNT_ Unmount_filesystems"/>
2. Resolve album Update issues
Updating album information in the Android4.4 version does not support folders and only supports individual file updates.
Sendbroadcast (New Intent (Intent.action_media_scanner_scan_file, Uri.parse ("file://" + FILE));
Although it is not possible to scan a folder, we can scan the file by traversing the folder sequentially.
1 //Single File Broadcast2 Private voidfilescan (String file) {3Groupactivity. This. Sendbroadcast (NewIntent (Intent.action_media_scanner_scan_file, Uri.parse ("file://"+file ));4 }5 //Traverse Folder6 Private voidFolderscan (String path) {7File File =NewFile (path);8 if(File.exists () &&file.isdirectory ()) {9file[] Array =file.listfiles ();Ten for(intI=0; i<array.length;i++){ OneFile f =Array[i]; A if(F.isfile ()) { -String name =f.getname (); - if(Name.endswith (". PNG") || Name.endswith (". jpg")){ the FileScan (F.getabsolutepath ()); - } - } - Else { + Folderscan (F.getabsolutepath ()); - } + } A } at}
General photo Information saved in the DCIM directory, update this directory to
1 String file= environment.getexternalstoragedirectory (). GetAbsolutePath () + "/dcim"; 2 Folderscan (file);