English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
When making an APP, when it comes to selecting local images, the system album is undoubtedly the first choice. However, with the variety of Android phones and the improvement of mobile phone pixels, factors such as large images cannot be returned and other anomalies make it difficult to adapt to different models. WeChat and QQ have successively integrated the image selection function into their own APPs and abandoned the system-provided image selector. Here, an image selector for local images is仿 made after QQ, PS: Someone said that '仿' was written as '防' before, so I paid special attention to it today, hoping not to make a mistake.
First, let's take a look at an effect diagram, a picture is worth a thousand words~~~
The effect achieved is roughly like this:
1.Single selection: Go to the local image selection folder, select a folder, and then enter all the images under the folder. After selecting a picture, return to the picture address information
2.Multiple selection: Go to the image folder, select a folder, select an image, click the small circle in the upper right corner, select the image, click on another area of the image, view the large image, click preview, view selected images, and you can select images across folders.
To achieve this effect, several things need to be done:
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, grouping by folder and returning the number of images under each folder.
2.Read all images under the specified folder:
When selecting a directory, it is necessary to read all the images under the directory.
String[] columns = new String[]{MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA}; /*Query images whose file paths contain the specified folder path above--In this way, it can ensure that the files queried belong to the current folder.*/ String whereclause = MediaStore.Images.ImageColumns.DATA + " like '" + folderPath + "/%'"; Log.i("queryGalleryPicture", "galleryPath:") + folderPath); Cursor cursor = c.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, whereclause, null, null);
The query statement here is filtered according to the filename, as long as the image path contains the folder path, it is the image under the folder
3.UI interaction
Firstly, when displaying all folders containing images, the folders with images are loaded asynchronously, and after reading successfully, the list is displayed. Here, RecyclerView is used to display list information. When clicking on a directory, read the images under the directory. In the image display page, it is necessary to note that each time an image is clicked, it is judged whether the current clicked image is already in the selection list. If it is, it is deleted; if not, it is added. Here, the ImageLoader open-source framework is used for image loading
if (mSelectlist.contains(imageBean)) { //Delete the clicked item if it is an already selected image mSelectlist.remove(imageBean); subSelectPosition(); } //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(); } //Notify that the clicked item has changed notifyItemChanged(position);
At the same time, when one image is deleted, the corresponding serial number on the image is changed, and then the UI update is notified.
When jumping between different Activities, because a list of image lists needs to be passed, and the list contains a custom entity class, it was initially considered to use intent to pass, but after passing through list.get(positon).contains to compare whether it is the same object, it is always a different object, everyone can go and verify. Therefore, an observer class is defined here to save the selected images and all images in the folder, and when viewing the large image, if one image is selected or one image is canceled, the observer can notify the update.
/** * Notify that the image selection has changed */ public void updateImageSelectChanged () { setChanged(); notifyObservers(imgSelectObj); }
Well, with the above, you can use our image selector:
When single selection is made, call at the required place:
/*Single selection, the parameters correspond to context, callback*/ FolderListActivity.startSelectSingleImgActivity(this, 2); When multiple selections are made: /*The parameters correspond to context, callback code, the passed image list, and the optional maximum number of images*/ FolderListActivity.startFolderListActivity(this, 1, null, 9);
Finally, receive the returned image data in the onActivityResult of Activity:
List<ImageFolderBean> list = (List<ImageFolderBean>) data.getSerializableExtra("list");
Source code download address:
https://github.com/JarekWang/photoselect
The above is the full description of the Android high imitation QQ picture selector introduced by the editor, hoping it will be helpful to everyone!
Declaration: The content of this article is from the network, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, has not been edited by humans, nor does it assume relevant legal liability. If you find any suspected copyright content, please send an email to: notice#oldtoolbag.com (When sending an email, please replace # with @ to report, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)