获取手机全部图片

这边图和效果就用其中一栏展示了
部分效果

具体就看下面代码了!


获取手机全部图片

想要展示呢第一步就需要先获取全部图片

下面是获取全部图片的代码
我写了一个类,来获取全部的图片
PhotoAlbumUtils.java

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * PhotoAlbumUtils
 * Author: gjn.
 * Time: 2018/4/3.
 */

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));
                    map.put(parent, photos);
                }
            }
            cursor.close();
        }
        return map;
    }

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

        public Photo() {
        }

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

        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;
        }
    }
}

很简单的代码,我这边就提及下就好了。
getContentResolver中检索全部图片的游标然后将图片的DATA(图片完全路径)、BUCKET_DISPLAY_NAME(图片处于哪个文件夹下)和SIZE(图片大小)取出来。然后这边我是创建一个Map按父文件名为key值保存key底下的全部文件,这边加了一句

    if (size > 20 * 1024){
        photos.add(new Photo(path, name, parent, parentPath, size));
        map.put(parent, photos);
    }

用来排除小于20kb的图片,因为手机里面有一些很小的图片可以让他们不显示出来这个只是按需求修改即可。


优化显示

由上面我们获取到了全部图片文件的路径。那么我们需要展示图片的话只要直接设置图片就好了。

这个时候我们可以直接使用Glide展示,简单又方便。

展示的时候这边使用的是RecyclerView,并且每个item都加入了一个CheckBox,然后由于RecyclerView的复用,如果只是单独的设置CheckBox,会导致item多了之后会选中情况会乱掉。

所以我们需要设置默认值,并且设置一个List只有在List内才可以设置CheckBox的状态为选中。

下面是实现代码

    //初始化
    private List<String> select = new ArrayList<>();
    //RecyclerView内的设置情况
    ((CheckBox) holder.getView(R.id.cb_ipa))
            .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        if (!select.contains(item.getPath())) {
                            select.add(item.getPath());
                        }
                    } else {
                        if (select.contains(item.getPath())) {
                            select.remove(item.getPath());
                        }
                    }
                }
            });
    holder.getView(R.id.cb_ipa).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (select.size() > 10) {
                ((CheckBox) holder.getView(R.id.cb_ipa)).setChecked(false);
                showToast("超过10张照片");
            }
        }
    });
    if (select.contains(item.getPath())) {
        ((CheckBox) holder.getView(R.id.cb_ipa)).setChecked(true);
    } else {
        ((CheckBox) holder.getView(R.id.cb_ipa)).setChecked(false);
    }

全部代码

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gjn.msdemo.R;
import com.gjn.msdemo.base.BaseActivity;
import com.gjn.msdemo.base.BaseRecyclerAdapter;
import com.gjn.msdemo.base.RecyclerViewHolder;
import com.gjn.msdemo.util.PhotoAlbumUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import butterknife.BindView;
import butterknife.OnClick;

public class PhotoAlbumActivity extends BaseActivity {
    @BindView(R.id.tv_title_apa)
    TextView tv_title_apa;
    @BindView(R.id.rv_apa)
    RecyclerView rv_apa;
    private BaseRecyclerAdapter<PhotoAlbumUtils.Photo> adapter;
    private Map<String, List<PhotoAlbumUtils.Photo>> map;
    private List<PhotoAlbumUtils.Photo> all;
    private List<String> select = new ArrayList<>();

    @Override
    protected void create() {
    }

    @Override
    protected void initview() {
        rv_apa.setLayoutManager(new GridLayoutManager(this, 3));
        adapter = new BaseRecyclerAdapter<PhotoAlbumUtils.Photo>(this, R.layout.item_photo_album, null) {
            @Override
            public void bindData(RecyclerViewHolder holder, PhotoAlbumUtils.Photo item,
                                 int position) {
                Glide.with(mContext).load(item.getPath())
                        .into(holder.getImageView(R.id.iv_ipa));
                ((CheckBox) holder.getView(R.id.cb_ipa))
                        .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                if (isChecked) {
                                    if (!select.contains(item.getPath())) {
                                        select.add(item.getPath());
                                    }
                                } else {
                                    if (select.contains(item.getPath())) {
                                        select.remove(item.getPath());
                                    }
                                }
                            }
                        });
                holder.getView(R.id.cb_ipa).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (select.size() > 10) {
                            ((CheckBox) holder.getView(R.id.cb_ipa)).setChecked(false);
                            showToast("超过10张照片");
                        }
                    }
                });
                if (select.contains(item.getPath())) {
                    ((CheckBox) holder.getView(R.id.cb_ipa)).setChecked(true);
                } else {
                    ((CheckBox) holder.getView(R.id.cb_ipa)).setChecked(false);
                }
            }
        };
        rv_apa.setAdapter(adapter);
    }

    @Override
    protected void initdata() {
        map = PhotoAlbumUtils.getPhotoAlbum(this);
        addAll();
    }

    private void addAll() {
        all = new ArrayList<>();
        map.entrySet().forEach(new Consumer<Map.Entry<String, List<PhotoAlbumUtils.Photo>>>() {
            @Override
            public void accept(Map.Entry<String, List<PhotoAlbumUtils.Photo>> entry) {
                all.addAll(entry.getValue());
            }
        });
        all.forEach(new Consumer<PhotoAlbumUtils.Photo>() {
            @Override
            public void accept(PhotoAlbumUtils.Photo photo) {
                adapter.add(photo);
            }
        });
    }

    @OnClick(R.id.tv_add_apa)
    public void add() {
        showToast("选中" + select.size() + "张图片");
        for (String s : select) {
            Log.e("-s-", "=>" + s);
        }
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_photo_album;
    }
}

xml布局

activity_photo_album.xml
这里写图片描述

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.gjn.msdemo.photo.PhotoAlbumActivity">

    <TextView
        android:id="@+id/tv_back_apa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="12dp"
        android:text="取消"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_add_apa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:layout_marginTop="12dp"
        android:text="确认"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_title_apa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="全部照片"
        android:textAllCaps="false"
        android:textColor="@android:color/black"
        android:textSize="22sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_apa"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_title_apa" />
</android.support.constraint.ConstraintLayout>

item_photo_album.xml
这里写图片描述

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_margin="10dp">

    <ImageView
        android:id="@+id/iv_ipa"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@color/backbg" />

    <CheckBox
        android:id="@+id/cb_ipa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right" />

</FrameLayout>

猜你喜欢

转载自blog.csdn.net/g777520/article/details/79881575
今日推荐