From 4dbb5a9f2bfd3b8e81ebd6ca74de415b1727fca1 Mon Sep 17 00:00:00 2001 From: liweijie Date: Mon, 8 Jun 2020 18:18:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E5=9B=BE=E7=89=87=E7=9A=84mimeType=E8=BF=87?= =?UTF-8?q?=E6=BB=A4=E5=8A=A0=E8=BD=BD=EF=BC=8C=E9=81=BF=E5=85=8D=E4=B8=8D?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E7=9A=84=E5=9B=BE=E7=89=87=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E4=B9=9F=E5=8A=A0=E8=BD=BD=E5=87=BA=E6=9D=A5=E4=BA=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../imagepickerdemo/ImagePickerActivity.java | 16 ++++++++++++--- .../com/lzy/imagepicker/ImageDataSource.java | 8 ++++++-- .../java/com/lzy/imagepicker/ImagePicker.java | 9 ++++++++- .../com/lzy/imagepicker/ImageTypeFilter.java | 20 +++++++++++++++++++ 4 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 imagepicker/src/main/java/com/lzy/imagepicker/ImageTypeFilter.java diff --git a/app/src/main/java/com/lzy/imagepickerdemo/ImagePickerActivity.java b/app/src/main/java/com/lzy/imagepickerdemo/ImagePickerActivity.java index 3972368..cdb7660 100644 --- a/app/src/main/java/com/lzy/imagepickerdemo/ImagePickerActivity.java +++ b/app/src/main/java/com/lzy/imagepickerdemo/ImagePickerActivity.java @@ -21,6 +21,7 @@ import com.isseiaoki.simplecropview.FreeCropImageView; import com.lzy.imagepicker.ImagePicker; +import com.lzy.imagepicker.ImageTypeFilter; import com.lzy.imagepicker.bean.ImageItem; import com.lzy.imagepicker.ui.ImageGridActivity; import com.lzy.imagepicker.util.InnerToaster; @@ -86,6 +87,15 @@ public void show(int resId) { Toast.makeText(ImagePickerActivity.this, resId, Toast.LENGTH_SHORT).show(); } }); + //添加类型过滤器 + imagePicker.setFilter(new ImageTypeFilter() { + @Override + public boolean filter(String mimeType) { + return "image/png".equalsIgnoreCase(mimeType) + || "image/jpeg".equalsIgnoreCase(mimeType) + || "image/jpg".equalsIgnoreCase(mimeType); + } + }); rb_uil = (RadioButton) findViewById(R.id.rb_uil); rb_glide = (RadioButton) findViewById(R.id.rb_glide); rb_picasso = (RadioButton) findViewById(R.id.rb_picasso); @@ -102,7 +112,7 @@ public void show(int resId) { rb_muti_select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton aCompoundButton, boolean aB) { - if (images != null&&aB) { + if (images != null && aB) { images.clear(); } } @@ -141,7 +151,7 @@ public void onCheckedChanged(CompoundButton aCompoundButton, boolean aB) { btn_open_gallery.setOnClickListener(this); Button btn_wxDemo = (Button) findViewById(R.id.btn_wxDemo); btn_wxDemo.setOnClickListener(this); - imagePicker.setCropCacheFolder(new File(getExternalCacheDir(),"test/crop")); + imagePicker.setCropCacheFolder(new File(getExternalCacheDir(), "test/crop")); gridView = (GridView) findViewById(R.id.gridview); } @@ -180,7 +190,7 @@ else if (rb_xutils3.isChecked()) imagePicker.setOutPutX(Integer.valueOf(et_outputx.getText().toString())); imagePicker.setOutPutY(Integer.valueOf(et_outputy.getText().toString())); - if (!imagePicker.isMultiMode()&&images!=null) { + if (!imagePicker.isMultiMode() && images != null) { images.clear(); } Intent intent = new Intent(this, ImageGridActivity.class); diff --git a/imagepicker/src/main/java/com/lzy/imagepicker/ImageDataSource.java b/imagepicker/src/main/java/com/lzy/imagepicker/ImageDataSource.java index 2911c31..b9952f4 100644 --- a/imagepicker/src/main/java/com/lzy/imagepicker/ImageDataSource.java +++ b/imagepicker/src/main/java/com/lzy/imagepicker/ImageDataSource.java @@ -34,13 +34,14 @@ public class ImageDataSource implements LoaderManager.LoaderCallbacks { private OnImagesLoadedListener loadedListener; private ArrayList imageFolders = new ArrayList<>(); private int mLoadedCount = 0; + private ImageTypeFilter mFilter; public ImageDataSource(FragmentActivity activity, String path, OnImagesLoadedListener loadedListener) { this.activity = activity; this.loadedListener = loadedListener; - mLoadedCount=0; - + mLoadedCount = 0; + mFilter = ImagePicker.getInstance().getFilter(); LoaderManager loaderManager = activity.getSupportLoaderManager(); if (path == null) { loaderManager.initLoader(LOADER_ALL, null, this); @@ -87,6 +88,9 @@ public void onLoadFinished(Loader loader, Cursor data) { int imageHeight = data.getInt(data.getColumnIndexOrThrow(IMAGE_PROJECTION[4])); String imageMimeType = data.getString(data.getColumnIndexOrThrow(IMAGE_PROJECTION[5])); long imageAddTime = data.getLong(data.getColumnIndexOrThrow(IMAGE_PROJECTION[6])); + if (mFilter != null && !mFilter.filter(imageMimeType)) { + continue; + } ImageItem imageItem = new ImageItem(); imageItem.name = imageName; imageItem.path = imagePath; diff --git a/imagepicker/src/main/java/com/lzy/imagepicker/ImagePicker.java b/imagepicker/src/main/java/com/lzy/imagepicker/ImagePicker.java index 6c8f181..05aa0e9 100644 --- a/imagepicker/src/main/java/com/lzy/imagepicker/ImagePicker.java +++ b/imagepicker/src/main/java/com/lzy/imagepicker/ImagePicker.java @@ -65,7 +65,7 @@ public class ImagePicker { private List mImageFolders; private int mCurrentImageFolderPosition = 0; private List mImageSelectedListeners; - + private ImageTypeFilter mFilter; private static ImagePicker mInstance; @@ -226,6 +226,13 @@ public void clearSelectedImages() { if (mSelectedImages != null) mSelectedImages.clear(); } + public ImageTypeFilter getFilter(){ + return mFilter; + } + + public void setFilter(ImageTypeFilter filter) { + this.mFilter = filter; + } public void clear() { if (mImageSelectedListeners != null) { mImageSelectedListeners.clear(); diff --git a/imagepicker/src/main/java/com/lzy/imagepicker/ImageTypeFilter.java b/imagepicker/src/main/java/com/lzy/imagepicker/ImageTypeFilter.java new file mode 100644 index 0000000..3324bf1 --- /dev/null +++ b/imagepicker/src/main/java/com/lzy/imagepicker/ImageTypeFilter.java @@ -0,0 +1,20 @@ +package com.lzy.imagepicker; + +/** + * Project Name: ImagePicker + * + * @author vj + * @date : 2020-06-08 18:09 + * email:liweijieok@qq.com + * desc: + * lastModify: + */ +public interface ImageTypeFilter { + /** + * filter + * + * @param mimeType image/png,image/jpeg,image/*,image/jpeg,image/webp,image/gif so... + * @return true:不过滤,会显示出来,false:过滤,不会加载出来 + */ + boolean filter(String mimeType); +}