【问题标题】:Android: getting image locationsAndroid:获取图像位置
【发布时间】:2009-11-27 23:21:04
【问题描述】:

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

理想的解决方案是获取图像的文件路径或 URI 集合。我猜你可以通过 MediaStore 做到这一点,我只是不知道怎么做。

谢谢

【问题讨论】:

    标签: android image mediastore


    【解决方案1】:

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

    返回SD卡上所有图片的Uri或者缩略图的Uri。

    【讨论】:

    • 最好使用 MediaStore.Images.Media.BUCKET_DISPLAY_NAME 过滤掉照片,而不是读取 uri
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多