2011-11-07 70 views
3

我能夠從我的SD卡中選擇所有的桶顯示名稱,現在我想從這個桶中創建縮略圖列表。我怎樣才能做到這一點?Android:列出每個桶的縮略圖

android的縮略圖表沒有任何信息可以用來查詢Images表。我試圖從MediaStore.Images.Media.EXTERNAL_CONTENT_URI獲取縮略圖ID,但這種情況下的_ID與縮略圖ID不同。

謝謝 牛逼

回答

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; 
} 

希望幫助

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