我正在寻找一种在SD卡上查找图像的方法,或者至少是相机拍摄的图像。

理想的解决方案是获取图像的文件路径或URI的集合。我猜你可以通过MediaStore做到这一点我无法弄清楚如何。

由于

有帮助吗?

解决方案

在查看了更多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。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top