MediaStore: get image data, thumbnail and folder

寵の児 提交于 2019-11-30 20:05:43

Here the code I've written:

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

It uses my ALbum class and previously declared var mAlbumsList but I think it's clear enough to understand how it works. Maybe it'll help someone.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!