Android:列出每个桶的缩略图

时间:2011-11-07 21:48:59

标签: android

我能够从SD卡中选择所有存储桶显示名称,现在我想从这个存储桶中构建一个缩略图列表。我怎样才能做到这一点?

android中的缩略图表没有任何可用于查询Images表的信息。我试图从MediaStore.Images.Media.EXTERNAL_CONTENT_URI获取缩略图ID,但在这种情况下_ID与缩略图ID不同。

谢谢 T

2 个答案:

答案 0 :(得分:4)

我遇到了同样的问题。图像和缩略图之间的公共值是图像ID,在图像中以_id形式存储,并作为image_id缩略图存储。我最后做的是在图像表中查询属于该存储桶的所有图像,然后查询缩略图表以查找具有匹配的image_id的所有缩略图。这是一些代码。

private void populateImages()
{
    int[] imageIds = getImagesFromBucket();
    String searchParams = null;
    if(imageIds != null && imageIds.length > 0)
    {
        searchParams = "";
        for(int i=0;i<imageIds.length;i++)
        {
            searchParams += "image_id = "+imageIds[i]+" OR ";
        }
        searchParams = searchParams.substring(0,searchParams.length() - 4);//cuts off the extra " OR "
    }
    String[] projection = {MediaStore.Images.Thumbnails._ID};
    cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
            projection,
            searchParams,
            null,
            MediaStore.Images.Thumbnails.IMAGE_ID);
    //do something with the result here
}


private int[] getImagesFromBucket() 
{
    int[] ids = null;
    ArrayList<Integer> lstIds = new ArrayList<Integer>();
    String searchParams = null;
    String bucket = getIntent().getExtras().getString("BucketName");
    if(bucket != null && !bucket.equals("All"))
    {
        searchParams = "bucket_display_name = \""+bucket+"\"";
    }
    else
    {
        return ids;
    }
    String[] projection = {MediaStore.Images.Media._ID};
    cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            projection,
            searchParams,
            null,null);
    if(cursor.moveToFirst())
    {
        do
        {
            int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media._ID));
            lstIds.add(id);
        }
        while(cursor.moveToNext());
    }
    ids = new int[lstIds.size()];
    for(int i=0;i<ids.length;i++)
    {
        ids[i] = (int)(lstIds.get(i));
    }
    return ids;
}

希望有所帮助

答案 1 :(得分:0)

public static void queryPhotoByBucket(Context context, String bucketId,
            ArrayList<UploadItem> photoItems) {
        photoItems.clear();
        Cursor c = null;
        Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        String[] projection = { MediaStore.Images.ImageColumns.DATA,
                MediaStore.Images.ImageColumns.DATE_TAKEN,
                MediaStore.Images.ImageColumns.DATE_ADDED,
                MediaStore.Images.Media._ID };
        String selection = MediaStore.Images.ImageColumns.BUCKET_ID
                + " = '" + bucketId + "' and "
                + MediaStore.Images.ImageColumns.DATE_TAKEN + " >= 0";
        if (u != null) {
            c = context.getContentResolver().query(u, projection, selection,
                    null, null);
        }
        if ((c != null) && (c.moveToFirst())) {
            do {
                photoItems.add(new UploadItem(c.getString(0),
                        new ImmutableDate(
                                Long.parseLong(c.getString(1) == null ? c
                                        .getString(2) : c.getString(1))), c
                                .getLong(3)));
            } while (c.moveToNext());
        }
        c.close();
        getThumbs(context, photoItems);
    }

    private static void getThumbs(Context context,
            ArrayList<UploadItem> photoItems) {
        for (int i = 0; i < photoItems.size(); i++) {
            UploadItem item = photoItems.get(i);
            getThumb(context, item);
        }
    }

    private static long lastPhotoId = -1;

    private static void getThumb(Context context, UploadItem item) {
        Cursor cursor = MediaStore.Images.Thumbnails.queryMiniThumbnail(
                context.getContentResolver(), item.getPhotoId(),
                MediaStore.Images.Thumbnails.MINI_KIND, null);
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            String thumbPath = cursor.getString(cursor
                    .getColumnIndex(MediaStore.Images.Thumbnails.DATA));
            item.setThumbPath(thumbPath);
        } else {
            if (lastPhotoId == item.getPhotoId()) {
                item.setThumbPath(item.getPhotoPath());
            } else {
                Thumbnails.getThumbnail(context.getContentResolver(),
                        item.getPhotoId(), Thumbnails.MINI_KIND, null);
                lastPhotoId = item.getPhotoId();
                getThumb(context, item);
            }
        }
    }