1. Overview
Should be the company's project needs, to do a picture selector, online search for some source code, I have been modified on the basis of others, the other page has been restructured, I am loading the image Folder List, and then enter the selection of pictures.
Reference Blog Address: http://blog.csdn.net/lmj623565791/article/details/39943731
Fix bug: Can scan gif file, and suffix name is uppercase or can be scanned out for example: image. Gif
As follows:
2. Scan the image folder core code, is to open a thread to scan all the pictures of the database, and then find their parent path, then go to heavy, with handle update the UI
- New Thread (New Runnable () {
- @Override
- public void Run () {
- String firstimage = null;
- Uri Mimageuri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
- Contentresolver mcontentresolver = ImageFolderActivity.this.getContentResolver ();
- Query only JPEG and PNG and GIF pictures
- Cursor mcursor = mcontentresolver.query (Mimageuri, NULL,
- MediaStore.Images.Media.MIME_TYPE + "=? or "
- + MediaStore.Images.Media.MIME_TYPE + "=? or "+mediastore.images.media.mime_type +" =? ",
- New string[] {"Image/jpeg", "Image/png", "Image/gif"},
- MediaStore.Images.Media.DATE_MODIFIED);
- LOG.E ("TAG", Mcursor.getcount () + "");
- hashset<string> mdirpaths = new hashset<string> ();//Temporary auxiliary class to prevent multiple scans of the same folder
- while (Mcursor.movetonext ()) {
- Get the path to a picture
- String path = mcursor.getstring (Mcursor.getcolumnindex (MediaStore.Images.Media.DATA));
- LOG.E ("TAG", Path);
- Get the path to the first picture
- if (firstimage = = null)
- Firstimage = path;
- Gets the parent path name of the picture
- File Parentfile = new file (path). Getparentfile ();
- if (Parentfile = = null)
- Continue
- String Dirpath = Parentfile.getabsolutepath ();
- Imagefloder imagefloder = null;
- Use a hashset to prevent multiple scans of the same folder (without this judgment, the picture is still quite scary ~ ~)
- if (Mdirpaths.contains (Dirpath)) {
- Continue
- } else {
- Mdirpaths.add (Dirpath);
- Initialize Imagefloder
- Imagefloder = new Imagefloder ();
- Imagefloder.setdir (Dirpath);
- Imagefloder.setfirstimagepath (path);
- }
- Get the number of current directory slices
- int picsize = parentfile.list (new FilenameFilter () {
- @Override
- Public Boolean Accept (File dir, String fileName) {
- Return Util.isimage (FileName);
- }
- }). length;
- Imagefloder.setcount (picsize);
- Mimagefloders.add (Imagefloder);
- }
- Mcursor.close ();
- Notify handler scan Picture complete
- Mhandler.sendemptymessage (0x110);
- }
- }). Start ();
3. The picture list is to filter all the files in the current folder according to the name suffix, and then display them. Put it in a GridView.
- File Dirfile=new file (Getintent (). Getstringextra (Constant.dir));
- Images = Arrays.aslist (dirfile.list (new FilenameFilter () {
- @Override
- Public Boolean Accept (File dir, String filename) {
- return util.isimage (filename);
- }
- }));
Click to download the source code
Android Image Selector