获取中所有图片展示

xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_al_bum"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:fitsSystemWindows="true"
    tools:context="com.mornningtime.clighten.active.AlBumActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/image_title"
        android:background="@color/redxy">

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="22dp"
            android:layout_height="22dp"
            android:layout_centerVertical="true"
            android:background="@null"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="@dimen/dp16"
            android:src="@mipmap/icon_jiantou" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:gravity="center_vertical"
            android:text="所有图片"
            android:textColor="@color/white"
            android:textSize="16sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:text="完成"
            android:id="@+id/image_next"
            android:gravity="center_vertical"
            android:textColor="@color/white"
            android:layout_marginRight="16dp"
            android:textSize="16sp"/>

    </RelativeLayout>

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/image_title"
        android:id="@+id/recycManager"></android.support.v7.widget.RecyclerView>

</RelativeLayout>

item-xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:scaleType="centerCrop"
        android:id="@+id/all_album_adapter_item_iv"/>

    <CheckBox
        android:layout_width="40dp"
        android:layout_height="30dp"
        android:layout_marginLeft="70dp"
        android:theme="@style/My_CheckBox"
        android:id="@+id/all_album_adapter_item_cb"/>
</FrameLayout>

adapter:

public class AllAlbumAdapter extends RecyclerView.Adapter<AllAlbumAdapter.ViewHolder>{
    private Context context;
    private List<PhotoAlbumUtils.Photo> all;
    private List<String> select = new ArrayList<>();
    private Bitmap bitmap;
    Dialog dialog=null;

    public AllAlbumAdapter(Context context, List<PhotoAlbumUtils.Photo> all) {
        this.context = context;
        this.all = all;
    }


    @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.all_album_adapter_item, null);
        return new ViewHolder(view);
    }


    @Override public void onBindViewHolder(final ViewHolder holder, final int position) {
        bitmap= BitmapFactory.decodeFile(all.get(position).getPath());
        holder.AllAlbumAdapterItemIv.setImageBitmap(bitmap);
        final ImageView imageView=getView();


        holder.AllAlbumAdapterItemCb.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                    if (holder.AllAlbumAdapterItemCb.isChecked()) {
                        if (select.size() ==9) {
                            holder.AllAlbumAdapterItemCb.setChecked(false);
                            Toast.makeText(context,"不能超过9张照片",Toast.LENGTH_SHORT).show();
                        }else {
                            if (!select.contains(all.get(position).getPath())) {
                                select.add(all.get(position).getPath());
                            }
                        }
                    } else {
                        if (select.contains(all.get(position).getPath())) {
                            select.remove(all.get(position).getPath());
                        }
                    }

            }
        });
        holder.AllAlbumAdapterItemIv.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                dialog=new Dialog(context,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
                dialog.setContentView(imageView);
                dialog.show();
            }
        });
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    @Override public int getItemCount() {
        return all==null?0:all.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        private ImageView AllAlbumAdapterItemIv;
        private CheckBox AllAlbumAdapterItemCb;

        public ViewHolder(View itemView) {
            super(itemView);
            AllAlbumAdapterItemIv=itemView.findViewById(R.id.all_album_adapter_item_iv);
            AllAlbumAdapterItemCb=itemView.findViewById(R.id.all_album_adapter_item_cb);
        }
    }

    private ImageView getView() {
        ImageView imgView = new ImageView(context);
        imgView.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.MATCH_PARENT));
        imgView.setImageBitmap(bitmap);
        return imgView;
    }
}

获取手机图片工具类:

public class PhotoAlbumUtils {

    public static Map<String, List<Photo>> getPhotoAlbum(Context context){
        Map<String, List<Photo>> map = new HashMap<>();
        Cursor cursor = context.getContentResolver()
            .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            while (cursor.moveToNext()) {
                String path = cursor.getString(
                    cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                String parent = cursor.getString(
                    cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME));
                int size = cursor.getInt(
                    cursor.getColumnIndex(MediaStore.Images.Media.SIZE));

                String name = Uri.parse(path).getLastPathSegment();
                String parentPath = path.replace(name,"");

                List<Photo> photos = new ArrayList<>();
                if (map.containsKey(parent)) {
                    photos = map.get(parent);
                }
                if (size > 20 * 1024){
                    photos.add(new Photo(path, name, parent, parentPath, size,false));
                    map.put(parent, photos);
                }
            }
            cursor.close();
        }
        return map;
    }

    public static class Photo{
        String path;
        String name;
        String parent;
        String parentPath;
        int size;
        boolean flag;

        public boolean isFlag() {
            return flag;
        }


        public void setFlag(boolean flag) {
            this.flag = flag;
        }


        public Photo(String path, String name, String parent, String parentPath, int size, boolean flag) {
            this.path = path;
            this.name = name;
            this.parent = parent;
            this.parentPath = parentPath;
            this.size = size;
            this.flag = flag;
        }

        public String getPath() {
            return path;
        }

        public void setPath(String path) {
            this.path = path;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getParent() {
            return parent;
        }

        public void setParent(String parent) {
            this.parent = parent;
        }

        public String getParentPath() {
            return parentPath;
        }

        public void setParentPath(String parentPath) {
            this.parentPath = parentPath;
        }

        public int getSize() {
            return size;
        }

        public void setSize(int size) {
            this.size = size;
        }
    }

activity:

public class AlBumActivity extends AppCompatActivity {
    private ImageButton imageButton;
    private TextView ImageNext;
    private Map<String, List<PhotoAlbumUtils.Photo>> map;
    private List<PhotoAlbumUtils.Photo> all=new ArrayList<>();
    private RecyclerView RecycManager;
    private AllAlbumAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_al_bum);
        imageButton=findViewById(R.id.imageButton);
        ImageNext=findViewById(R.id.image_next);
        RecycManager=findViewById(R.id.recycManager);

        map = PhotoAlbumUtils.getPhotoAlbum(this);
        Set<Map.Entry<String, List<PhotoAlbumUtils.Photo>>> set = map.entrySet();
        for (Map.Entry<String, List<PhotoAlbumUtils.Photo>> me : set) {
            // 根据键值对对象获取键和值
            String key = me.getKey();
            all.addAll(me.getValue());
        }

        RecycManager.setLayoutManager(new GridLayoutManager(AlBumActivity.this,4));
        RecycManager.addItemDecoration(new DividerItemDecoration(this,
            DividerItemDecoration.VERTICAL_LIST));
        adapter=new AllAlbumAdapter(AlBumActivity.this,all);
        RecycManager.setAdapter(adapter);
        adapter.notifyDataSetChanged();

        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                finish();
            }
        });

        ImageNext.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {

            }
        });
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值