get MediaStore Album Titles - Android Media

Android examples for Media:Album

Description

get MediaStore Album Titles

Demo Code


import java.io.File;
import java.util.LinkedList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.MediaColumns;

public class Main{
    public static List<AlbumListing> getAlbumTitles(final Context aContext) {

        final Cursor cursor = aContext.getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new String[] {
                        MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
                        MediaStore.Images.ImageColumns.BUCKET_ID }, null,
                null, MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME);

        final List<AlbumListing> titles = new LinkedList<AlbumListing>();
        if (null == cursor) {
            return titles;
        }/*from   w  w w .j  a  v  a2 s  .  com*/

        AlbumListing previous = null;
        try {
            while (cursor.moveToNext()) {
                final String name = cursor.getString(0);
                final int id = cursor.getInt(1);

                if (null != previous && previous.name.equals(name)) {
                    continue;
                }

                final AlbumListing album = new AlbumListing(name, id);
                titles.add(album);
                previous = album;
            }
        } finally {
            cursor.close();
        }

        return titles;
    }
}

Related Tutorials