Imitation QQ image selector-Android,

Source: Internet
Author: User

Imitation QQ image selector-Android,

As an APP, when you need to select a local image, the first consideration is undoubtedly the system album, but the Android mobile phone is varied, and the pixel increase of the mobile phone, the big picture cannot return and other exception factors, as a result, it is difficult to adapt to the model. QQ has successively integrated the Image Selection Function in its own APP and gave up the image selector provided by the system. Here QQ is a local image selector, PS: Someone said that "Imitation" was written as "anti-DDoS". I paid special attention to it today.

First upload the image, no picture, no truth ~~~

The effect is probably as follows:

1. single choice: Jump to the Local Image Selection folder, select a folder, go to all the images under the folder, select an image, return to change the image address information

2. select multiple: Go to the image folder, select a folder, select an image, click the small circle in the upper right corner, select the image, click other areas of the image, view the large image, and click preview to view the selected image, you can select images across folders.

 

To achieve this, you need to do the following:

1. Read all local folders with images:

Here, ContentResolver is used to read media files.

 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 that groups folders and returns the number of folder slices.

2. Read all images in the specified folder:

When selecting a directory, you need to read all the images in the directory.

String [] columns = new String [] {MediaStore. images. media. _ ID, MediaStore. images. media. DATA};/* the query file path contains the image of the folder path specified above -- this ensures that the file to be queried 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 statements here are filtered Based on the file name. If the image path contains the folder path, it is the image in the folder.

3. UI Interaction

First, when displaying all folders containing images, the folder with images is asynchronously loaded. After reading the folder, the list is displayed. RecyclerView is used to display the list information. When you click a directory, on the Image Display page, you must note that each time you click to determine whether the currently clicked image is already in the selection list. If it is in, delete it, or not, add. Here, the image is loaded using the open-source framework ImageLoader.

If (mSelectlist. contains (imageBean) {// when the item is selected, delete mSelectlist. remove (imageBean); subSelectPosition ();} else {// not in the selection 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 () ;}// the notification click item has changed notifyItemChanged (position );

At the same time, no image is deleted, the serial number on the image is changed accordingly, and the UI is updated by notifying the change item.

 

When redirecting from different activities, the List of images must be passed. The list contains a custom object class. intent transfer is considered at first, but after intent is passed, the list is passed. get (positon ). when contains compares whether the same object is always a different object, you can verify it. Therefore, an observer class is defined here to save the selected image and all the images in the folder. When you view the larger image, if you select one or cancel the selected image, update with the observer notification.

/*** The notification image has been changed */public void updateImageSelectChanged () {setChanged (); yyobservers (imgSelectObj );}

 

Now, with the above options, we can use our image selector:

For single choice, you can call it as needed:

/* Single-choice, the parameter corresponds to context, callback */FolderListActivity. startSelectSingleImgActivity (this, 2 );

When multiple options are selected:

/* Parameter corresponds to context, callback code, input image List, maximum number of optional items */FolderListActivity. startFolderListActivity (this, 1, null, 9 );

Finally, the onActivityResult of the Activity receives the returned image data:

 List<ImageFolderBean> list = (List<ImageFolderBean>) data.getSerializableExtra("list");

 

Source code:

Https://github.com/JarekWang/photoselect

 

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.