Android Image Selector

Source: Internet
Author: User

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

  1. New Thread (New Runnable () {
  2. @Override
  3. public void Run () {
  4. String firstimage = null;
  5. Uri Mimageuri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  6. Contentresolver mcontentresolver = ImageFolderActivity.this.getContentResolver ();
  7. Query only JPEG and PNG and GIF pictures
  8. Cursor mcursor = mcontentresolver.query (Mimageuri, NULL,
  9. MediaStore.Images.Media.MIME_TYPE + "=? or "
  10. + MediaStore.Images.Media.MIME_TYPE + "=? or "+mediastore.images.media.mime_type +" =? ",
  11. New string[] {"Image/jpeg", "Image/png", "Image/gif"},
  12. MediaStore.Images.Media.DATE_MODIFIED);
  13. LOG.E ("TAG", Mcursor.getcount () + "");
  14. hashset<string> mdirpaths = new hashset<string> ();//Temporary auxiliary class to prevent multiple scans of the same folder
  15. while (Mcursor.movetonext ()) {
  16. Get the path to a picture
  17. String path = mcursor.getstring (Mcursor.getcolumnindex (MediaStore.Images.Media.DATA));
  18. LOG.E ("TAG", Path);
  19. Get the path to the first picture
  20. if (firstimage = = null)
  21. Firstimage = path;
  22. Gets the parent path name of the picture
  23. File Parentfile = new file (path). Getparentfile ();
  24. if (Parentfile = = null)
  25. Continue
  26. String Dirpath = Parentfile.getabsolutepath ();
  27. Imagefloder imagefloder = null;
  28. Use a hashset to prevent multiple scans of the same folder (without this judgment, the picture is still quite scary ~ ~)
  29. if (Mdirpaths.contains (Dirpath)) {
  30. Continue
  31. } else {
  32. Mdirpaths.add (Dirpath);
  33. Initialize Imagefloder
  34. Imagefloder = new Imagefloder ();
  35. Imagefloder.setdir (Dirpath);
  36. Imagefloder.setfirstimagepath (path);
  37. }
  38. Get the number of current directory slices
  39. int picsize = parentfile.list (new FilenameFilter () {
  40. @Override
  41. Public Boolean Accept (File dir, String fileName) {
  42. Return Util.isimage (FileName);
  43. }
  44. }). length;
  45. Imagefloder.setcount (picsize);
  46. Mimagefloders.add (Imagefloder);
  47. }
  48. Mcursor.close ();
  49. Notify handler scan Picture complete
  50. Mhandler.sendemptymessage (0x110);
  51. }
  52. }). 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.

    1. File Dirfile=new file (Getintent (). Getstringextra (Constant.dir));
    2. Images = Arrays.aslist (dirfile.list (new FilenameFilter () {
    3. @Override
    4. Public Boolean Accept (File dir, String filename) {
    5. return util.isimage (filename);
    6. }
    7. }));

Click to download the source code

Android Image Selector

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.