Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 增加可以根据图片的mimeType过滤加载,避免不需要的图片类型也加载出来了。 #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions app/src/main/java/com/lzy/imagepickerdemo/ImagePickerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ public class ImageDataSource implements LoaderManager.LoaderCallbacks<Cursor> {
private OnImagesLoadedListener loadedListener;
private ArrayList<ImageFolder> 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);
Expand Down Expand Up @@ -87,6 +88,9 @@ public void onLoadFinished(Loader<Cursor> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class ImagePicker {
private List<ImageFolder> mImageFolders;
private int mCurrentImageFolderPosition = 0;
private List<OnImageSelectedListener> mImageSelectedListeners;

private ImageTypeFilter mFilter;
private static ImagePicker mInstance;


Expand Down Expand Up @@ -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();
Expand Down
20 changes: 20 additions & 0 deletions imagepicker/src/main/java/com/lzy/imagepicker/ImageTypeFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.lzy.imagepicker;

/**
* Project Name: ImagePicker
*
* @author vj
* @date : 2020-06-08 18:09
* email:[email protected]
* 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);
}