I haven't written an article for a long time. Recently, I have been busy with some things about the company's desktop maintenance. A few days ago, I received a task to write a copy of WeChat to select multiple pictures. Today, I will summarize it. I found a very useful image selection library. Let's talk about the usage of this library:
1. Grab and import the Jar package through Gradle (Glide and RecyclerView, there is a connection at the end of the article)
compile 'com.yancy.imageselector:imageselector:1.1.0'
2. Add the following permissions to AndroidManifest.xml
<!-- Permission to read data from sdcard--> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!-- Permission to write data to sdcard--> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
3. Create an image loader (Glide is recommended )
public class GlideLoader implements com.yancy.imageselector.ImageLoader { @Override public void displayImage(Context context, String path, ImageView imageView) { Glide.with(context) .load(path) .placeholder(com.yancy.imageselector.R.mipmap.imageselector_photo) .centerCrop() .into(imageView); } }
4. Configure ImageSelector
ImageConfig imageConfig = new ImageConfig.Builder(MainActivity.this , new GlideLoader()) // If it is above 4.4, modify the status bar color (default black) .steepToolBarColor(getResources().getColor(R.color.blue)) // background color of the title (default black) .titleBgColor(getResources().getColor(R.color.blue)) // The color of the submit button font (default white) .titleSubmitTextColor(getResources().getColor(R.color.white)) // title color (default white) .titleTextColor(getResources().getColor(R.color.white)) // Enable multiple selection (multiple selection by default) (singleSelect for single selection) .mutiSelect() // Maximum number of multiple selections (default 9) .mutiSelectMaxSize(9) // selected image path .pathList(path) // The image path stored after taking a photo (default /temp/picture) .filePath("/ImageSelector/Pictures") // Enable the camera function (off by default) .showCamera() .build(); ImageSelector.open(imageConfig); // Open the image selector
5. Get the selected photo path array in onActivityResult:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == ImageSelector.IMAGE_REQUEST_CODE && resultCode == RESULT_OK && data != ) { // Get Image Path List List<String> pathList = data.getStringArrayListExtra(ImageSelectorActivity.EXTRA_RESULT); for (String path : pathList) { Log.e("The path of the selected picture is -----", path); } } }