Imitation QQ picture Selector--android

Source: Internet
Author: User

As an app, need to select local pictures, the first consideration is undoubtedly the system album, but Android phone a variety of, and then the mobile phone pixel upgrade, large map can not return such abnormal factors, resulting in a more difficult to adapt to the model, QQ has been in their own app integrated image selection function, Give up the system to provide the picture selector, here imitation QQ made a local image selector, PS: Before someone said "imitation" written "anti", today deliberately pay attention to the next, beg good.

First one, no picture no truth Ah ~ ~ ~

The effect of the implementation is probably this:

1. Single: Jump to the local image Select folder, select a folder, go to all the pictures under the folder, select a picture, return to change the image address information

2. Multi-select: Jump to the Picture folder, select a folder, select a picture, click on the top right corner of the small circle, select the image, click on the image of other areas, view large image, click Preview, view the selected picture, you can select the picture across folders.

In order to achieve this effect, a few things need to be done:

1. Read all the local folders with pictures:

Read media files here with Contentresolver

string[] Columns = {mediastore.images.media._id, MediaStore.Images.Thumbnails.DATA, MediaStore.Images.Media.DATA, MediaStore.Images.Media.BUCKET_ID,                        "count (1) as Count "  "0==0) GROUP by (" +=null, SortOrder);

This is a simple SQL query statement, grouped by folder, and returns the number of folder slices.

2. Read all the pictures in the specified folder:

When you select a directory, you need to read all the pictures in that directory.

string[] Columns =Newstring[]{mediastore.images.media._id, MediaStore.Images.Media.DATA};/*The query file path contains a picture of the folder path specified above-to ensure that the queried file belongs to the current folder*/String Whereclause= MediaStore.Images.ImageColumns.DATA +"Like '"+ FolderPath +"/% '"; LOG.I ("querygallerypicture","Gallerypath:"+FolderPath); CursorCorsor= C.getcontentresolver (). Query (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, Whereclause,NULL, NULL);

The query statement here is filtered according to the file name, as long as the picture path contains the folder path, then the picture under that folder

3.UI interaction

First, in the display of all folders containing pictures, asynchronously loaded with pictures of the folder, read successfully after the list display, where the Recyclerview display list information, click on a directory, in the reading of the directory under the picture, in the Picture Display page, it is important to note that Each click to determine whether the current click on the picture is in the selection list, if in, delete, not, add. Here the picture is loaded with the open source framework Imageloader

                 if(Mselectlist.contains (Imagebean)) {//When you click the item for the selected picture, deleteMselectlist.remove (Imagebean);                 Subselectposition (); } Else{//not in the select list, add                     if(Mselectlist.size () >=MaxCount) {Toast.maketext (Mcontext, Mcontext.getresources (). getString (R.string.publish_select_photo_max, MA                         Xcount), Toast.length_short). Show (); return;                     } mselectlist.add (Imagebean); Imagebean.selectposition=mselectlist.size (); }                 //Notice that the click item has changedNotifyitemchanged (position);

Also did not delete a picture, the number of the image on the corresponding changes, and then notify the change to update the UI.

When different activity jumps, because to pass the picture list list,list is the custom entity class, has just started to consider passes with the intent, but intent passes through List.get (positon). Contains compare whether the same object is is always different object, everyone can go to verify under. So here is an observer's class, to save the selected pictures and all the pictures under the folder, while looking at the larger image, if you select a picture or deselect a picture, notify the update by the Observer

    /**      * Notification picture selection has changed      *    /publicvoid  updateimageselectchanged () {        Setchanged ();        Notifyobservers (imgselectobj);    }

Well, with the above, you can use our image selector:

When the radio is on, call it where it is needed:

   /* single selection, the parameter corresponds to the context, callback */         folderlistactivity.startselectsingleimgactivity (this, 2);

When selecting multiple:

/* parameters correspond to context, callback code, incoming picture list, optional maximum number of sheets */         folderlistactivity.startfolderlistactivity (thisnull, 9);

The last activity of the Onactivityresult receives the returned picture data:

list<imagefolderbean> list = (list<imagefolderbean>) data.getserializableextra ("list");

Source:

Https://github.com/JarekWang/photoselect

Imitation QQ picture Selector--android

Related Article

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.