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