图片选择器满足你的一切需求,单选、多选、裁剪、拍照、九图、图片预览、单击放大、一应俱全,适配到Android8.0。如果UI色调不符合你的项目需求,可继承AbstractXXXActivity,重写相关布局,并在ImagePicker.getInstance().startXXX()方法中传入自定义的Activity。
在使用ImagePicker前,必须在Activity中重写onActivityResult方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
ImagePicker.getInstance().onActivityResult(requestCode, resultCode, data);
}
其次实现ImageLoader,并设置到ImagePicker.getInstance().imageLoader(new GlideImageLoader())
public class GlideImageLoader implements ImageLoader {
@Override
public void displayFileImage(ImageView imageView, String path) {
GlideApp.with(imageView.getContext())
.load(Uri.fromFile(new File(path)))
.into(imageView);
}
@Override
public void displayUserImage(ImageView imageView, String path) {
GlideApp.with(imageView.getContext()).load(path).into(imageView);
}
@Override
public void displayFileVideo(String path) {
}
@Override
public Class<?> displayFullImageClass() {
return null;
}
}
ImagePicker.getInstance()
.crop(false)//是否裁剪
.selectedListener(new ImagePicker.OnSelectedListener() {
@Override
public void onImageSelected(List<ImageItem> items) {
toast(items.toString());
}
})
.startPhotoPicker(this);
ImagePicker.getInstance()
.crop(true)//是否裁剪
.outPutY(800)//裁剪大小
.outPutX(800)
.selectedListener(new ImagePicker.OnSelectedListener() {
@Override
public void onImageSelected(List<ImageItem> items) {
toast(items.toString());
}
})
.startPhotoPicker(this);
ImagePicker.getInstance()
.multiMode(false)//是否多选
.showCamera(true)//是否显示拍照Item
.crop(false)//是否裁剪
.outPutY(800)
.outPutX(800)
.selectedListener(new ImagePicker.OnSelectedListener() {
@Override
public void onImageSelected(List<ImageItem> items) {
toast(items.toString());
}
})
.startImagePicker(this);
ImagePicker.getInstance()
.multiMode(true)//是否多选
.selectLimit(8)//图片限制
.showCamera(true)
.selectedListener(new ImagePicker.OnSelectedListener() {
@Override
public void onImageSelected(List<ImageItem> items) {
toast(items.toString());
}
})
.startImagePicker(this, null);//第二个参数为已经选择的图片路径集合
@BindLayout(R.layout.activity_sample_nine)
public class SampleNineActivity extends AppCompatActivity {
RecyclerView rc_view;
ImageAdapter imageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample_nine);
rc_view = findViewbyId(R.id.rc_view);
imageAdapter = new ImageAdapter(this);
imageAdapter.setRecyclerView(rc_view);
imageAdapter.setImageSize(20, 0, 0);//九图间隔;距离左边;距离右边
List<String> list = new ArrayList<>();
list.add("http://dpic.tiankong.com/8n/a0/QJ6177688033.jpg");
list.add("http://dpic.tiankong.com/6j/s5/QJ6146491299.jpg");
list.add("http://dpic.tiankong.com/zt/pz/QJ6519443181.jpg");
list.add("http://dpic.tiankong.com/br/ja/QJ6223394486.jpg");
list.add("http://dpic.tiankong.com/i6/ke/QJ6322734186.jpg");
list.add("http://dpic.tiankong.com/cl/at/QJ6769314375.jpg");
list.add("http://dpic.tiankong.com/xp/3t/QJ6230352510.jpg");
list.add("http://dpic.tiankong.com/37/ha/QJ6534800776.jpg");
list.add("http://dpic.tiankong.com/5p/33/QJ6215180920.jpg");
imageAdapter.bindData(list);
}
@Override
public void onActivityReenter(int resultCode, Intent data) {
super.onActivityReenter(resultCode, data);
imageAdapter.onActivityReenter(resultCode, data);
}
}
List<String> list = new ArrayList<>();
list.add("http://dpic.tiankong.com/8n/a0/QJ6177688033.jpg");
list.add("http://dpic.tiankong.com/6j/s5/QJ6146491299.jpg");
list.add("http://dpic.tiankong.com/zt/pz/QJ6519443181.jpg");
list.add("http://dpic.tiankong.com/br/ja/QJ6223394486.jpg");
list.add("http://dpic.tiankong.com/i6/ke/QJ6322734186.jpg");
list.add("http://dpic.tiankong.com/cl/at/QJ6769314375.jpg");
list.add("http://dpic.tiankong.com/xp/3t/QJ6230352510.jpg");
list.add("http://dpic.tiankong.com/37/ha/QJ6534800776.jpg");
list.add("http://dpic.tiankong.com/5p/33/QJ6215180920.jpg");
ImagePicker.getInstance().startImageViewer(this, list);
ImagePicker.getInstance()
.selectedListener(new ImagePicker.OnSelectedListener() {
@Override
public void onImageSelected(List<ImageItem> items) {
toast(items.toString());
}
})
.startVideoPicker(this);
dependencies {
...
implementation 'com.hacknife:imagepicker:1.3.1'
}