MediaStore: get image data, thumbnail and folder

后端 未结 1 915
失恋的感觉
失恋的感觉 2021-01-05 02:58

I have two lists. Let\'s call them AlbumsList and PicturesList.

  • The first one shows photo album cover (one of the images from it) it\'s name and number of pic
相关标签:
1条回答
  • 2021-01-05 03:27

    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.

    0 讨论(0)
提交回复
热议问题