我只想检索存储桶的名称(专辑)。例如。相机、下载等但不是所有照片的相机、下载等列表,那么如何为每个存储桶名称检索一行?
我的意思是像在 Gallery 应用程序中,你首先有相册,例如相机。当你点击它时,它会显示相机的所有照片。
我可以使用查询的 Where 子句查询相机胶卷中的照片。但是,如果我只想要每个相册的名称而不是照片,是否可以查询呢?如果我查询所有的照片,每组照片只取一行,那会很耗时。
请帮忙
最佳答案
我有同样的问题,这是我的解决方案(在跟踪图库源代码之后)获取相册名称和其中的第一张图片(可以用作该相册的缩略图):
(请注意,桶重复已通过 groupby 和排序技术删除)
// 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());
}
关于Android:如何查询存储桶名称列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11570521/