Android High imitation QQ picture selector _android

Source: Internet
Author: User

As an app, need to select a local picture, the first consideration is undoubtedly the system album, but the Android phone is a variety of, plus the mobile pixel upgrade, the big picture can not return the abnormal factors, resulting in the adaptation of the model is more difficult, micro-mail, QQ are successively in their own app integrated picture selection function, Gave up the system to provide the picture selector, here imitation QQ made a local picture selector, PS: Before someone said "imitation" written "prevention", today deliberately pay attention to the next, please.

First on the effect of the picture, no picture without the truth Ah ~ ~ ~

The effect of the implementation is probably this:

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

2. Multiple selection: Jump to the Picture folder, select a folder, select the picture, click on the small circle in the upper right corner, select the map, click on other areas of the picture, view the larger picture, click Preview, view the selected pictures, you can select pictures across folders.

To achieve this effect, several things need to be done:

1. Read all folders with pictures locally:

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,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME, "count (1) as COUNT"};
String selection = "0==0" GROUP by ("+ MediaStore.Images.Media.BUCKET_ID;
String sortOrder = MediaStore.Images.Media.DATE_MODIFIED;
Cursor Cursor = Contentresolver.query (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, selection, NULL, SortOrder);

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

2. Read all pictures under the specified folder:

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

string[] columns = new string[]{mediastore.images.media._id, MediaStore.Images.Media.DATA};
/* Query file path contains a picture of the folder path specified above-this ensures that the queried file belongs to the current folder/
String whereclause = MediaStore.Images.ImageColumns.DATA + " Like ' "+ FolderPath +"/% ' ";
LOG.I ("Querygallerypicture", "Gallerypath:" + folderpath);
Cursor corsor = 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, 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 the list after the success of the display, where the Recyclerview display list information, click on a directory, in the reading of the image under the change directory, in the Picture Display page, it is necessary to note that, Each click to determine whether the current click on the picture is already in the select list, if in, delete, not, add. Here the picture is loaded with an open source framework Imageloader

if (Mselectlist.contains (Imagebean)) {//Click the item for the selected picture when deleting
mselectlist.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, Maxcount), Toast.length_short). Show ();
return;
}
Mselectlist.add (Imagebean);
Imagebean.selectposition = Mselectlist.size ();
}
Notice that the click item has changed
notifyitemchanged (position);

At the same time did not delete a picture, the number of the image on the corresponding changes, and then notify the change item update UI.

Different activity jumps, because to pass the picture list list,list is a custom entity class, just began to consider using intent delivery, but intent passed through the List.get (positon). When comparing the same object, the contains are always different objects, you can go to verify. So here defines an observer's class, to save all the pictures in the selected pictures and folders, while looking at the larger picture, if you select one or deselect a picture, notify the update through the Observer

/**
* Notification picture selection has changed/public
void updateimageselectchanged () {
setchanged ();
Notifyobservers (imgselectobj);
}

Well, with the above, we can use our picture selector:

When the radio is in place, call it where needed:

/* Single selection, the parameter corresponds to the context, callback
/Folderlistactivity.startselectsingleimgactivity (this, 2);
Multi-selection:
/* parameter corresponding to the context, callback code, the incoming picture list, the maximum number of cards can be selected
/folderlistactivity.startfolderlistactivity (this, 1, NULL, 9 );

Receive the returned picture data in the onactivityresult of the last activity:

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

SOURCE Download Address:

Https://github.com/JarekWang/photoselect

The above is a small set to introduce the Android high imitation QQ picture selector of all the narration, I hope to help you!

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.