2009-11-27 188 views
2

我正在尋找一種方法來查找SD卡上的圖像,或至少是相機拍攝的圖像。Android:獲取圖像位置

理想的解決方案是獲取圖像的文件路徑或URI的集合。我猜你可以通過MediaStore做到這一點,我只是無法弄清楚。

感謝

回答

4

尋找更多MediaStore的例子後,我來到了這一點,它能夠完成任務。

protected ArrayList<Uri> GetImageList(boolean getThumbs) 
{  
    ArrayList<Uri> images = new ArrayList<Uri>(); 
    String columnType; 
    Uri contentType;     
    String[] columnNames; 

    if (getThumbs) 
    { 
     columnType = MediaStore.Images.Thumbnails._ID; 
     contentType = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI; 
    } 
    else 
    { 
     columnType = MediaStore.Images.Media._ID; 
     contentType = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 
    } 

    columnNames = new String[]{columnType};    
    Cursor cursor = managedQuery(contentType, columnNames, null, null, null);  
    int columnIndex = cursor.getColumnIndexOrThrow(columnType); 

    for(int i = 0; i < cursor.getCount(); i++) 
    { 
     cursor.moveToPosition(i); 
     int id = cursor.getInt(columnIndex); 
     images.add(Uri.withAppendedPath(contentType, "" + id)); 
    } 

    return images; 
} 

它返回所有圖像的Uri或SD卡上所有圖像的縮略圖的Uri。

+1

最好是使用MediaStore.Images.Media.BUCKET_DISPLAY_NAME過濾掉照片,而不是讀取uri – shem 2013-02-05 13:43:25