Android Imitation micro-letter Photo Selector Implementation Preview View picture _android

Source: Internet
Author: User

Okay, let's go to the bottom of the list, and we'll see how it works:

Here's how to introduce the code:

This idea is:

    • 1. Scan JPEG and PNG images on mobile phones first
    • 2. Get the path of the guide picture and the parent path name of the picture, which is the folder name
    • 3. Add the picture path and folder name to the data source separately
    • 4. The data source has the display, the folder display is utilizes the Popwindow, but the picture display is the GridView

Take a look at the specific code:

First open a thread to scan the picture

/** * Use ContentProvider scan mobile phone pictures, this method in the child thread to complete the scan of the picture, and eventually get the most JPG folder * * private void GetImages () {if (! Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {Toast.maketext (this, "temporary no external storage", TOAST.L 
  Ength_short). Show (); 
 Return 
 
 ///Show progress bar Mprogressdialog = Progressdialog.show (this, null, "Loading ..."); 
 
  New Thread (New Runnable () {@Override public void run () {String firstimage = null; 
  Uri Mimageuri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 
 
  Contentresolver mcontentresolver = albumactivity.this. Getcontentresolver (); Query only JPEG and png pictures Cursor mcursor = mcontentresolver.query (Mimageuri, NULL, MediaStore.Images.Media.MIME_TYPE +) =? or "+ MediaStore.Images.Media.MIME_TYPE +" =? ", new string[] {" Image/jpeg "," Image/png "}, Mediastore.image 
 
  s.media.date_modified); 
  LOG.E ("TAG", Mcursor.getcount () + ""); while (Mcursor.movetonext ()) {//Get the path of the picture String path = Mcursor.getString (mcursor. Getcolumnindex (MediaStore.Images.Media.DATA)); 
   LOG.E ("TAG", Path); 
   Get the first picture of the path 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 more or less scary ~ ~) if (Mdirpaths.contains (Dirpath)) {continue; 
   else {mdirpaths.add (dirpath); 
   Initialize Imagefloder Imagefloder = new Imagefloder (); 
   Imagefloder.setdir (Dirpath); 
   Imagefloder.setfirstimagepath (path); int picsize = parentfile.list (new FilenameFilter () {@Override public boolean accept (File dir, String  
    FileName) {if (Filename.endswith (". jpg") | | filename.endswith (". png") | | filename.endswith (". jpeg") 
    return true; 
   return false; 
   }). length; 
 
   TotalCount + = Picsize; ImageflOder.setcount (picsize); 
 
   Mimagefloders.add (Imagefloder); 
   if (Picsize > mpicssize) {mpicssize = picsize; 
   Mimgdir = Parentfile; 
 
  } mcursor.close (); 
 
  The scan completes, the auxiliary HashSet also can release the memory mdirpaths = NULL; 
 
  Notify handler scan picture complete mhandler.sendemptymessage (0x110); 
 
 }). Start (); }

The code is very detailed, not much.
Folder Popwindow Pop-up Event

private void Initevent () 
 { 
 /** 
  * For the bottom of the layout set click event, Pop-up Popupwindow * * * 
 Mbottomly.setonclicklistener ( New View.onclicklistener () 
 { 
  @Override public 
  void OnClick (View v) 
  { 
  Mlistimagedirpopupwindow 
   . Setanimationstyle (r.style.anim_popup_dir); 
  Mlistimagedirpopupwindow.showasdropdown (mbottomly, 0, 0); 
 
  Set background color darken 
  windowmanager.layoutparams LP = GetWindow (). GetAttributes (); 
  Lp.alpha =. 3f; 
  GetWindow (). SetAttributes (LP); 
  } 
 }); 
 

Finally, set the Click event of the picture

Set ImageView Click event Mimageview.setonclicklistener (New Onclicklistener () {//Select to darken the picture and vice versa @Override public void OnClick (View v) {//has selected the picture if (Mselectedimage.contains (Mdirpath + "/" + Item)) {Mselectedimage. 
   Remove (Mdirpath + "/" + Item); 
   Mselect.setimageresource (r.drawable.picture_unselected); 
   Mimageview.setcolorfilter (NULL); 
   list<imagebean> Delete = new arraylist<imagebean> (); 
   for (Imagebean Im:Bimp.tempSelectBitmap) {if (Im.getpath (). Equals (Mdirpath + "/" + Item)) {Delete.add (IM); 
   } Bimp.tempSelectBitmap.removeAll (delete); 
   msg = new Message (); 
   msg.what=0; 
  AlbumActivity.handler.sendMessage (msg); else//did not select the picture {if (Bimp.tempSelectBitmap.size () >8) {Toast.maketext (context, "exceeds the number of optional pictures", Toast.length_sho 
   RT). Show (); 
   Return 
   else {Mselectedimage.add (Mdirpath + "/" + Item); 
   Mselect.setimageresource (r.drawable.pictures_selected); Mimageview.Setcolorfilter (Color.parsecolor ("#77000000")); 
   Imagebean Imagebean = new Imagebean (); 
   Imagebean.setpath (Mdirpath + "/" + Item); 
   try {imagebean.setbitmap (bimp.revitionimagesize (Mdirpath + "/" + Item)); 
   catch (IOException e) {e.printstacktrace (); 
   } Bimp.tempSelectBitmap.add (Imagebean); 
   msg = new Message (); 
   msg.what=0; 
   AlbumActivity.handler.sendMessage (msg);  } 
 
  } 
 
  }

This inside in order to match the previous blog, I joined the Select picture and uncheck the picture to delete and add the picture in Bimp.tempselectbitmap, update the number of selected pictures, that is, the following two pieces of code:

list<imagebean> Delete = new arraylist<imagebean> (); 
   for (Imagebean im:Bimp.tempSelectBitmap) { 
   if (Im.getpath (). Equals (Mdirpath + "/" + Item)) { 
    delete.add (IM); 
   } 
   } 
   Bimp.tempSelectBitmap.removeAll (delete); 
   msg = new Message (); 
   msg.what=0; 
   AlbumActivity.handler.sendMessage (msg); 
Imagebean Imagebean = new Imagebean (); 
   Imagebean.setpath (Mdirpath + "/" + item); 
   try { 
    Imagebean.setbitmap (bimp.revitionimagesize (Mdirpath + "/" + Item); 
   } catch (IOException e) { 
    E.printstacktrace (); 
   } 
   Bimp.tempSelectBitmap.add (Imagebean); 
   msg = new Message (); 
   msg.what=0; 
   AlbumActivity.handler.sendMessage (msg); 

Here is a note that I encountered a mistake in writing the removal of the picture, Java concurrentmodificationexception exception, this error is that when our vector, List or ArrayList in the data source changes, you can then operate this list will appear this error, the solution is to traverse the image array, compare the path is the same (the best way to compare the ID is the same), new An array will have the same picture if the new array, and then the last image array RemoveAll to remove, so it will not report an abnormal error, of course, our new array is certainly less than our previous array data sources or equivalent.

This is the entire content of this article, I hope to learn more about Android software programming help.

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.