In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the "how to use ImageSelector to achieve Wechat picture selector in Android" related knowledge, in the actual case of the operation process, many people will encounter such a dilemma, then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!
Introduce dependency
/ in the build.gradle of Project, add the following code allprojects {repositories {... Maven {url 'https://jitpack.io'} / / if you are using version 1.4.0 or earlier, this sentence may not be used. Maven {url 'https://maven.google.com'}} / / add the following code compile' com.github.donkingliang:ImageSelector:1.5.0' in the build.gradle of Module
Configure AndroidManifest.xml
/ / read permission of memory card / / Picture Select Activity// Picture Preview Activity// Picture cut Activity
Set up the picture selector
/ / Select ImageSelectorUtils.openPhoto (MainActivity.this, REQUEST_CODE, true, 0); / / limit the number of multiple selections (metaphor up to 9) ImageSelectorUtils.openPhoto (MainActivity.this, REQUEST_CODE, false, 9); ImageSelectorUtils.openPhoto (MainActivity.this, REQUEST_CODE, false, 9, selected); / / pass in the selected ones. / / an unlimited number of multiple-choice ImageSelectorUtils.openPhoto (MainActivity.this, REQUEST_CODE); ImageSelectorUtils.openPhoto (MainActivity.this, REQUEST_CODE, selected); / / pass in the selected ones. / / or ImageSelectorUtils.openPhoto (MainActivity.this, REQUEST_CODE, false, 0); ImageSelectorUtils.openPhoto (MainActivity.this, REQUEST_CODE, false, 0, selected); / / pass in the selected ones. / / Select and clip ImageSelectorUtils.openPhotoAndClip (MainActivity.this, REQUEST_CODE)
REQUEST_CODE is defined by the caller as the requestCode when starting Activity, which I believe everyone can understand. When you open the selector again, selected can pass in the previously selected images so that they are selected by default.
Receive the data returned by the selector
@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {super.onActivityResult (requestCode, resultCode, data); if (requestCode = = REQUEST_CODE & & data! = null) {/ / get the data returned by the selector ArrayList images = data.getStringArrayListExtra (ImageSelectorUtils.SELECT_RESULT);}}
ImageSelectorUtils.SELECT_RESULT is the key that receives data. The data is returned as a string array of ArrayList, and even if it is selected, the ArrayList array is returned, but at this time ArrayList has only one piece of data. The data in ArrayList is the file path of the selected image.
Are you a little confused? I attach the actual operation code.
1. Adapter_image.xml layout
two。 Main layout
3.ImageAdapter (picture selector utility class)
Public class ImageAdapter extends RecyclerView.Adapter {private Context mContext; private ArrayList mImages; private LayoutInflater mInflater; public ImageAdapter (Context context) {mContext = context; this.mInflater = LayoutInflater.from (mContext);} public ArrayList getImages () {return mImages;} @ Override public ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {View view = mInflater.inflate (R.layout.adapter_image, parent, false); return new ViewHolder (view) } @ Override public void onBindViewHolder (final ViewHolder holder, final int position) {final String image = mImages.get (position); Glide.with (mContext) .load (new File (image)) .into (holder.ivImage);} @ Override public int getItemCount () {return mImages = = null? 0: mImages.size ();} public void refresh (ArrayList images) {mImages = images; notifyDataSetChanged ();} static class ViewHolder extends RecyclerView.ViewHolder {ImageView ivImage; public ViewHolder (View itemView) {super (itemView) IvImage = itemView.findViewById (R.id.iv_image);}
4. Business logic
Package com.example.imageselector;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.GridLayoutManager;import android.support.v7.widget.RecyclerView;import android.view.View;import android.widget.Button;import com.donkingliang.imageselector.utils.ImageSelectorUtils;import java.util.ArrayList;import butterknife.BindView;import butterknife.ButterKnife;import butterknife.OnClick;public class MainActivity extends AppCompatActivity {@ BindView (R.id.btn_single) Button btnSingle @ BindView (R.id.btn_limit) Button btnLimit; @ BindView (R.id.btn_unlimited) Button btnUnlimited; @ BindView (R.id.btn_clip) Button btnClip; @ BindView (R.id.rv_image) RecyclerView rvImage; private static final int REQUEST_CODE = 0x00000011; private ImageAdapter mAdapter; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); ButterKnife.bind (this); rvImage.setLayoutManager (new GridLayoutManager (this, 3)) MAdapter = new ImageAdapter (this); rvImage.setAdapter (mAdapter);} @ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {super.onActivityResult (requestCode, resultCode, data); if (requestCode = = REQUEST_CODE & & data! = null) {ArrayList images = data.getStringArrayListExtra (ImageSelectorUtils.SELECT_RESULT); mAdapter.refresh (images) } @ OnClick ({R.id.btn_single, R.id.btn_limit, R.id.btn_unlimited, R.id.btn_clip}) public void onViewClicked (View view) {switch (view.getId ()) {case R.id.btn_single: / / Radio ImageSelectorUtils.openPhoto (MainActivity.this, REQUEST_CODE, true, 0); break Case R.id.btn_limit: / / multiple (up to 9) ImageSelectorUtils.openPhoto (MainActivity.this, REQUEST_CODE, false, 10); / / ImageSelectorUtils.openPhoto (MainActivity.this, REQUEST_CODE, false, 9, mAdapter.getImages ()); / / pass in the selected ones. Break; case R.id.btn_unlimited: / / multiple (unlimited) ImageSelectorUtils.openPhoto (MainActivity.this, REQUEST_CODE); / / ImageSelectorUtils.openPhoto (MainActivity.this, REQUEST_CODE, mAdapter.getImages ()); / / pass in the selected ones. / / or / ImageSelectorUtils.openPhoto (MainActivity.this, REQUEST_CODE, false, 0); / / ImageSelectorUtils.openPhoto (MainActivity.this, REQUEST_CODE, false, 0, mAdapter.getImages ()); / / pass in the selected ones. Break; case R.id.btn_clip: / / Select and clip ImageSelectorUtils.openPhotoAndClip (MainActivity.this, REQUEST_CODE); break;}} "how to use ImageSelector to implement Wechat Picture Selector in Android" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.