Android: How to query a list of bucket name

前端 未结 5 1891
后悔当初
后悔当初 2020-12-30 06:53

I want to retrieve only the name of the bucket (Albums). E.g. Camera, Download etc but not a list of Camera, Download etc from all the the photos so how do I retrieve one ro

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-30 07:32

    You can query MediaStore.Images.Media.BUCKET_DISPLAY_NAME for it. Peter Knego sent a good example. It's a good example and you can update it using query() from a ContentResolver object rather than managedQuery() that is deprecated.

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

提交回复
热议问题