这是我写的代码:
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.Images.Media._ID, MediaStore.Images.Media.BUCKET_ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
ArrayList<String> ids = new ArrayList<String>();
mAlbumsList.clear();
if (cursor != null) {
while (cursor.moveToNext()) {
Album album = new Album();
int columnIndex = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_ID);
album.id = cursor.getString(columnIndex);
if (!ids.contains(album.id)) {
columnIndex = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
album.name = cursor.getString(columnIndex);
columnIndex = cursor.getColumnIndex(MediaStore.Images.Media._ID);
album.coverID = cursor.getLong(columnIndex);
mAlbumsList.add(album);
ids.add(album.id);
} else {
mAlbumsList.get(ids.indexOf(album.id)).count++;
}
}
cursor.close();
它使用我的ALbum
类和之前声明的 varmAlbumsList
但我认为它很清楚可以理解它是如何工作的。也许它会帮助某人。