Android图片多选功能实现
package com.tdroid.imageselecotr; import android.content.Intent; import android.net.Uri; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import com.tdroid.imageselector.library.imageselelctor.ImageSelectorImageFromDialog; import com.tdroid.imageselector.library.imageselelctor.ImageSelectorView; import com.tdroid.imageselector.library.imageselelctor.SelectorBean; import java.io.File; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ImageSelectorView imageSelectorViewEditModel; private ImageSelectorView imageSelectorViewViewModel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageSelectorViewEditModel = (ImageSelectorView) findViewById(R.id.imageselector_editmodel); imageSelectorViewViewModel = (ImageSelectorView) findViewById(R.id.imageselector_viewmodel); setImageSelectorConfig(); loadImageSelectorSource(); } private void setImageSelectorConfig() { //初始化 imageSelectorViewEditModel.initSelector(this); imageSelectorViewViewModel.initSelector(this); //显示图片描述 imageSelectorViewEditModel.setShowDisc(true); //显示图片描述 imageSelectorViewViewModel.setShowDisc(true); //可编辑的imageviewselector监听设置 imageSelectorViewEditModel.setImageSelectorViewListener(new ImageSelectorView.ImageSelectorViewListener() { @Override public void onChoice(Intent intent, int intentTag, Uri uri) { //该方法会调用系统相机. intentTag=ImageSelectorImageFromDialog.CAMERA_INTENT_REQUEST //需要处理onActivityResult //防止部分手机调用系统相机时,activity会被销毁而不能获取到传入的图片路径. //会在onActivityResult里获取该值 // new SharedPreferencesUtils(ResureIdCardActivity.this).setTakeCameraSharePath(uri.getPath()); startActivityForResult(intent, intentTag); } @Override public void onDelete(int position, SelectorBean selectorBean) { //删除图片操作时,需要调用 imageSelectorViewEditModel.refreshDeleteResult(position); } }); //获取imageselector里最终的数据 List<selectorbean> selectorBeen=imageSelectorViewEditModel.getFinalImageList(); } //只负责显示的demo private void loadImageSelectorSource(){ //需要手动构建该集合 List<selectorbean> images = new ArrayList<>(); //测试2张数据 for(int i=0;i<2;i++){ String path=""; String dis="图片描述"; SelectorBean selectorBean=new SelectorBean(); if(i==0){ path="http://imgsrc.baidu.com/baike/pic/item/728da9773912b31b4d4df1ed8518367adbb4e1d5.jpg"; dis+="1"; } if(i==1){ path="http://e.hiphotos.baidu.com/image/pic/item/b17eca8065380cd785f7d9d6a644ad345982811b.jpg"; dis+="2"; } selectorBean.setDisc(dis); selectorBean.setPath(path); images.add(selectorBean); } imageSelectorViewViewModel.setSelectorData(images); // if (images.size() > 0) { // imageSelectorView.setVisibility(View.VISIBLE); // List<selectorbean> selectorBeanList = new ArrayList<selectorbean>(); // for (AskInfoImage image : images) { // SelectorBean selectorBean = new SelectorBean(); // selectorBean.setDisc((image.getDescription() == null) ? "" : image.getDescription()); // selectorBean.setPath((image.getImageUrl() == null) ? "" : image.getImageUrl()); // selectorBean.setIsAddTag(false); // selectorBeanList.add(selectorBean); // } // imageSelectorViewViewModel.setSelectorData(selectorBeanList); // } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { //相机 if (resultCode == RESULT_OK && requestCode == ImageSelectorImageFromDialog.CAMERA_INTENT_REQUEST) { try { //String path = new SharedPreferencesUtils(ResureIdCardActivity.this).getTakeCameraSharePath(); String path = "调用系统相继返回的图片路径"; File file = new File(path); if (file.exists()) { //需要手动给imageSelectorView 传入值显示相机返回图片 imageSelectorViewEditModel.setImageFromCamera(path); } } catch (Exception e) { e.printStackTrace(); } } } } </selectorbean></selectorbean></selectorbean></selectorbean>
资源均来自第三方,谨慎下载,前往第三方网站下载