获取手机相册的工具类

public class AlbumPhotoUtils {
    public static final int _20K = 20 * 1024;

    public static Map<String, List<Photo>> getAlbumPhoto(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 > _20K) {
                    photos.add(new Photo(path, name, parent, parentPath, size));
                    map.put(parent, photos);
                }
            }
            cursor.close();
        }
        return map;
    }
}

Photo.java

public 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;
        toString();
    }

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

    @Override
    public String toString() {
        return "Photo{" +
                "path='" + path + '\'' +
                ", name='" + name + '\'' +
                ", parent='" + parent + '\'' +
                ", parentPath='" + parentPath + '\'' +
                ", size=" + size +
                '}';
    }
}

遍历

 for (String key : mPhotoList.keySet()) {
            AlbumPhotoBean albumPhotoBean = new AlbumPhotoBean();
            albumPhotoBean.setFolderName(key);
            albumPhotoBean.setFiles(mPhotoList.get(key).size() + "");
            Logger.d(Logger._JN, "initViews  :%s ", albumPhotoBean);
            mFolderList.add(albumPhotoBean);
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值