12

我只想检索存储桶的名称(相册)。例如,相机、下载等,但不是所有照片中的相机、下载等列表,那么如何为每个存储桶名称分别检索一行?

我在图库应用程序中的意思是,您首先拥有相册,例如相机。当您单击它时,它会显示相机的所有照片。

我可以使用查询的 Where 子句查询相机胶卷中的照片。但是,如果我只想要每个专辑的名称而不是照片的名称,是否可以查询呢?如果我查询所有照片并且每组照片只拍摄一行,那么这将非常耗时。

请帮忙

4

5 回答 5

25

我有同样的问题,这是我的解决方案(在跟踪画廊源代码之后)来获取专辑名称和其中的第一张图片(可以用作这张专辑的缩略图):

(请注意,桶重复被 groupby 和 order 技术删除)

    // which image properties are we querying
    String[] PROJECTION_BUCKET = {
            ImageColumns.BUCKET_ID,
            ImageColumns.BUCKET_DISPLAY_NAME,
            ImageColumns.DATE_TAKEN,
            ImageColumns.DATA};
    // We want to order the albums by reverse chronological order. We abuse the
    // "WHERE" parameter to insert a "GROUP BY" clause into the SQL statement.
    // The template for "WHERE" parameter is like:
    //    SELECT ... FROM ... WHERE (%s)
    // and we make it look like:
    //    SELECT ... FROM ... WHERE (1) GROUP BY 1,(2)
    // The "(1)" means true. The "1,(2)" means the first two columns specified
    // after SELECT. Note that because there is a ")" in the template, we use
    // "(2" to match it.
    String BUCKET_GROUP_BY =
            "1) GROUP BY 1,(2";
    String BUCKET_ORDER_BY = "MAX(datetaken) DESC";

    // Get the base URI for the People table in the Contacts content provider.
    Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

    Cursor cur = getContentResolver().query(
            images, PROJECTION_BUCKET, BUCKET_GROUP_BY, null, BUCKET_ORDER_BY);

    Log.i("ListingImages"," query count=" + cur.getCount());

    if (cur.moveToFirst()) {
        String bucket;
        String date;
        String data;
        int bucketColumn = cur.getColumnIndex(
                MediaStore.Images.Media.BUCKET_DISPLAY_NAME);

        int dateColumn = cur.getColumnIndex(
                MediaStore.Images.Media.DATE_TAKEN);
        int dataColumn = cur.getColumnIndex(
                MediaStore.Images.Media.DATA);

        do {
            // Get the field values
            bucket = cur.getString(bucketColumn);
            date = cur.getString(dateColumn);
            data = cur.getString(dataColumn);

            // Do something with the values.
            Log.i("ListingImages", " bucket=" + bucket 
                    + "  date_taken=" + date
                    + "  _data=" + data);
        } while (cur.moveToNext());
    }
于 2013-12-10T05:05:18.373 回答
7

这里是。这个对我有用:

Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    String[] projection = new String[]{   
            MediaStore.Images.Media.BUCKET_ID,
            MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
            MediaStore.Images.Media.DATE_TAKEN,
            MediaStore.Images.Media.DATA
    };

    String BUCKET_ORDER_BY = MediaStore.Images.Media.DATE_MODIFIED + " DESC";
    String BUCKET_GROUP_BY = "1) GROUP BY 1,(2";
    Cursor imagecursor = managedQuery(images,
            projection, // Which columns to return
            BUCKET_GROUP_BY,       // Which rows to return (all rows)
            null,       // Selection arguments (none)
            BUCKET_ORDER_BY        // Ordering
            );

    this.imageUrls = new ArrayList<String>();
    this.imageBuckets  = new ArrayList<String>();
    for (int i = 0; i < imagecursor.getCount(); i++)
    {
        imagecursor.moveToPosition(i);
        int bucketColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
        String bucketDisplayName = imagecursor.getString(bucketColumnIndex);
        imageBuckets.add(bucketDisplayName);
        int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
        imageUrls.add(imagecursor.getString(dataColumnIndex));

    }

imageBuckets Arraylist 包含专辑名称

imageUrls Arraylist 包含专辑最后修改图像的路径,您可以将其用作缩略图

于 2014-09-30T12:33:08.103 回答
2

使用以下功能获取 Video 或 Images 专辑:

   /*
    *
    *   Author : @nieldeokar
    *   mediaType could be one of
    *
    *   public static final int MEDIA_TYPE_IMAGE = 1;
    *
    *   public static final int MEDIA_TYPE_VIDEO = 3;
    *
    *   from android.provider.MediaStore class
    *
    */
fun getAlbumList(mediaType: Int, contentResolver: ContentResolver) {
    val countColumnName = "count"
    var contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
    if (mediaType == MEDIA_TYPE_VIDEO) {
        contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
    }

    val projection = arrayOf(ImageColumns.BUCKET_ID, ImageColumns.BUCKET_DISPLAY_NAME, ImageColumns.DATE_TAKEN, ImageColumns.DATA)
    val bucketGroupBy = "1) GROUP BY ${ImageColumns.BUCKET_ID}, (${ImageColumns.BUCKET_DISPLAY_NAME}"
    val bucketOrderBy = MediaStore.Images.Media.DATE_MODIFIED + " DESC"

    val cursor = contentResolver.query(contentUri, projection, bucketGroupBy, null, bucketOrderBy)


    if (cursor != null) {
        while (cursor.moveToNext()) {
            val bucketId = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.BUCKET_ID))
            val name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME))
            val path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA)) // Thumb image path

            val selection = MediaStore.Images.Media.BUCKET_ID + "='" + bucketId + "'"

            val countCursor = contentResolver.query(contentUri, arrayOf( "count(" + MediaStore.Images.ImageColumns._ID + ")"), selection, null, null)

            var count = 0
            if (countCursor != null) {
                countCursor.moveToFirst()
                count = countCursor.getInt(0)
                countCursor.close()
            }

            Log.d("AlbumScanner", "bucketId : $bucketId | name : $name | count : $count | path : $path")
        }
        cursor.close()

    }

}

这形成了一个 SQL 语句:

SELECT bucket_id, bucket_display_name, datetaken, _data FROM images WHERE (1) GROUP BY bucket_id,(bucket_display_name) ORDER BY date_modified DESC

更新: 正如@PerracoLabs 指出的那样,此代码需要改进以针对Android Q

于 2019-03-06T15:45:10.473 回答
-1

您可以查询 MediaStore.Images.Media.BUCKET_DISPLAY_NAME 。Peter Knego提供了一个很好的例子。这是一个很好的示例,您可以使用 ContentResolver 对象中的 query() 来更新它,而不是使用已弃用的 managedQuery()。

// which image properties are we querying
String[] projection = new String[]{
        MediaStore.Images.Media._ID,
        MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
        MediaStore.Images.Media.DATE_TAKEN
};

// Get the base URI for the People table in the Contacts content provider.
Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

// Make the query.
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(images,
        projection, // Which columns to return
        "",         // Which rows to return (all rows)
        null,       // Selection arguments (none)
        ""          // Ordering
        );

Log.i("ListingImages"," query count="+cur.getCount());

if (cur.moveToFirst()) {
    String bucket;
    String date;
    int bucketColumn = cur.getColumnIndex(
        MediaStore.Images.Media.BUCKET_DISPLAY_NAME);

    int dateColumn = cur.getColumnIndex(
        MediaStore.Images.Media.DATE_TAKEN);

    do {
        // Get the field values
        bucket = cur.getString(bucketColumn);
        date = cur.getString(dateColumn);

        // Do something with the values.
        Log.i("ListingImages", " bucket=" + bucket 
               + "  date_taken=" + date);
    } while (cur.moveToNext());

}
于 2012-08-09T17:10:55.133 回答
-1

在查询中进行投影,如下所示:

String bucketProjection[] = {"Distinct "+ MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
于 2017-03-08T09:02:15.187 回答