做了个很简单的demo,适配器写好了相机相关的,但是功能没做,其他的根据需要去做优化
效果图如下:
需要权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
获取所有图片列表
/**
* 获取照片列表
* @param context
* @return
*/
public static List<PhotoPickBean> getAllPhoto(Context context){
List<PhotoPickBean> photoList = new ArrayList<>();
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA}, MediaStore.Images.Media.MIME_TYPE + "=? or " + MediaStore.Images.Media.MIME_TYPE + "=?", new String[]{"image/jpeg", "image/png"}, MediaStore.Images.Media._ID + " DESC");
PhotoPickBean photoPickBean;
if(cursor != null){
while (cursor.moveToNext()) {
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
File file = new File(path);
if (file.exists()) {
photoPickBean = new PhotoPickBean();
photoPickBean.setPhotoPath(path);
photoList.add(photoPickBean);
}
}
cursor.close();
}
DialogUtil.dismissLoading();
return photoList;
}
Adapter适配器
public class AdapterPhotoPicker extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private int ITEM_NORMAL = 1;
private int ITEM_CAMERA = 2;
private Context context;
private List<PhotoPickBean> dataList = new ArrayList<>();
private IPhotoPicker iPhotoPicker;
private LinkedHashMap<Integer, PhotoPickBean> pickMap = new LinkedHashMap<>();
public AdapterPhotoPicker(Context context) {
this.context = context;
}
public void setDataList(List<PhotoPickBean> dataList) {
this.dataList = dataList;
notifyDataSetChanged();
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if(viewType == ITEM_NORMAL)
return new PhotoVH(LayoutInflater.from(context).inflate(R.layout.item_photo, null));
else
return new PhotoVH(LayoutInflater.from(context).inflate(R.layout.item_photo_camera, null));
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
if(holder instanceof PhotoVH)
{
// 相册照片
PhotoVH h = (PhotoVH) holder;
h.rlPhoto.getLayoutParams().height = (int) ((MyApplication.getScreenWidth() - (context.getResources().getDimension(R.dimen.x30) * 4)) / 3);
h.rlPhoto.setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
if(pickMap.containsKey(position))
{
pickMap.remove(position);
}else
{
if(pickMap.size() >= Constant.ALBUM_PICK_COUNT_MAX)
{
ToastUtil.showToast(context, "已");
return;
}
pickMap.put(position, dataList.get(position));
}
calculateCheckDataIndex();
if(iPhotoPicker != null)
iPhotoPicker.clickItem(position);
notifyDataSetChanged();
}
});
if(pickMap.containsKey(position))
{
h.tvIndex.setSelected(true);
h.tvIndex.setText((dataList.get(position).getOperateIndex() + 1) + "");
}else{
h.tvIndex.setSelected(false);
h.tvIndex.setText("");
}
try {
File file = new File(dataList.get(position).getPhotoPath());
ImageLoader.load(file, h.rivPhoto);
}catch (Exception e){}
}else if(holder instanceof CameraVH)
{
// 相机
}
}
private void calculateCheckDataIndex() {
int p = 0;
for (Map.Entry<Integer, PhotoPickBean> map: pickMap.entrySet())
{
PhotoPickBean bean = map.getValue();
bean.setOperateIndex(p);
int k = map.getKey();
pickMap.put(k, bean);
p++;
}
}
public LinkedHashMap<Integer, PhotoPickBean> getCheckMap(){
return pickMap;
}
@Override
public int getItemViewType(int position) {
if(dataList.get(position).getType() == 0)
return ITEM_NORMAL;
else return ITEM_CAMERA;
}
@Override
public int getItemCount() {
return dataList.size();
}
public class PhotoVH extends RecyclerView.ViewHolder{
private RelativeLayout rlPhoto;
private RoundedImageView rivPhoto;
private TextView tvIndex;
public PhotoVH(@NonNull View itemView) {
super(itemView);
rlPhoto = itemView.findViewById(R.id.rlPhoto);
rivPhoto = itemView.findViewById(R.id.rivPhoto);
tvIndex = itemView.findViewById(R.id.tvIndex);
}
}
public class CameraVH extends RecyclerView.ViewHolder {
public CameraVH(@NonNull View itemView) {
super(itemView);
}
}
public interface IPhotoPicker {
void clickItem(int position); // 点击照片
void clickCamera(); // 点击相机
}
}
使用
private void initPhoto() {
DialogUtil.showLoading(this);
// 耗时操作,必须异步,也可以在getAllPhoto方法中进行异步,通过handler或接口进行回调,插入数据需要在ui进程,此处为了方便,简单处理,实际操作尽量不这么做
ablumThread = new Thread(new Runnable() {
@Override
public void run() {
photoList = SystemUtil.getAllPhoto(ActivityPhotoPicker.this);
runOnUiThread(new Runnable() {
@Override
public void run() {
if(photoList.size() > 0)
{
pickAdapter.setDataList(photoList);
}else
{
// 未查询到的处理
}
}
});
}
});
ablumThread.start();
}
bean 对象
public class PhotoPickBean {
private String photoPath;
private int operateIndex;
private int type; // 0照片 1相机
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getPhotoPath() {
return photoPath;
}
public void setPhotoPath(String photoPath) {
this.photoPath = photoPath;
}
public int getOperateIndex() {
return operateIndex;
}
public void setOperateIndex(int operateIndex) {
this.operateIndex = operateIndex;
}
}