Android picture picker, imitation of wechat picture picker style and effect. Support single image, limited number of multiple selection and unlimited number of multiple selection. Support picture preview and picture folder switch. Support picture selection and clipping.
First, the effect picture:
1. Introduce dependencies
Add the following code in Project build.gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io'} // If you are using version 1.4.0 or earlier, do not use this sentence. maven { url'https://maven.google.com'}}}Copy the code
Add the following code to build.gradle in Module
The compile 'com. Making. Donkingliang: ImageSelector: 1.5.0'Copy the code
Glide 4.x version is used in version 1.5.0. Since the Glide 3.x version is used differently than the 4.x version, if your project is using The Glide 3.x version and you do not want to upgrade to 4.x, you can also use ImageSelector:1.4.0. It is only the Glide version that is different from version 1.5.0.
2. Configure androidmanifest.xml
// Storage card read permission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"< Activity Android :name="com.donkingliang.imageselector.ImageSelectorActivity"// Remove the Activity's ActionBar. // the user can configure it according to their project. It is not necessary to write this, as long as the ActionBar of the not Activity is removed. android:theme="@style/Theme.AppCompat.Light.NoActionBar"// Switch between vertical and horizontal screens. // If you want to support vertical/horizontal switching, you must add this sentence, otherwise there will be an exception when switching horizontal/vertical. android:configChanges="orientation|keyboardHidden|screenSize"< Activity Android :name= /> //"com.donkingliang.imageselector.PreviewActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:configChanges="orientation|keyboardHidden|screenSize"Activity < Activity Android :name="com.donkingliang.imageselector.ClipImageActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
Copy the code
3. Turn up the picture picker
ImageSelector supports single selection, limited multiple selection, and unlimited multiple selection of images. You need to tell the selector when you’re setting up the picture selector that that’s the case. For your convenience, I’ve included a utility class in the project that makes it easy to call up the selector. It takes a simple line of code to call up the selector.
/ / radio ImageSelectorUtils. OpenPhoto (MainActivity. This REQUEST_CODE,true, 0); / / limit the number of alternative (9) most metaphor ImageSelectorUtils. OpenPhoto (MainActivity. This REQUEST_CODE,false, 9);
ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, false, 9, selected); // Pass in the selected one. / / an unlimited number of alternative ImageSelectorUtils. OpenPhoto (MainActivity. This REQUEST_CODE); ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, selected); // Pass in the selected one. / / or ImageSelectorUtils. OpenPhoto (MainActivity. This REQUEST_CODE,false, 0);
ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, false, 0, selected); // Pass in the selected one. / / radio and clipping ImageSelectorUtils. OpenPhotoAndClip (MainActivity. This REQUEST_CODE);Copy the code
REQUEST_CODE is the requestCode defined by the caller to start the Activity. Selected Allows you to pass in previously selected images when you open the selector again, making them default to the selected state.
4. Receive the data returned by the selector
Receive the data returned by the selector in the Activity’s onActivityResult method.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE && data ! = null) {/ / obtain the data selector returns the ArrayList < String > images. = data getStringArrayListExtra (ImageSelectorUtils. SELECT_RESULT); }}Copy the code
The key of ImageSelectorUtils SELECT_RESULT is receiving data. The data is returned as an array of strings from an ArrayList, or even radio, an ArrayList, except that the ArrayList has only one piece of data. The data in the ArrayList is the file path of the selected image.
The original blog: http://www.apkbus.com/blog-927916-77189.html