/** * 读取系统的 多媒体索引 获取 sd卡中的所有图片信息 */ public void initImagesIndex() { ImageCache. imgDirPathList = new ArrayList<String>(); ImageCache. imagesIndex = new HashMap<String, List<Map<String, String>>>(); ContentResolver cr = mContext.getContentResolver(); String[] imagesProjection = { Images.ImageColumns. _ID, Images.ImageColumns.DATA , Images.ImageColumns.BUCKET_DISPLAY_NAME }; // 查询sd上所有的图片信息 Cursor cur = cr.query(Images.Media. EXTERNAL_CONTENT_URI, imagesProjection, null , null, null); if (cur.moveToFirst()) { int _id; // 图片id String data; // 图片Path 具体路径 String dirName; // 图片所在目录的名称 int _idColumn = cur.getColumnIndex(Images.ImageColumns._ID ); int data_idColcum = cur.getColumnIndex(Images.ImageColumns.DATA ); int dispalyname_Column = cur.getColumnIndex(Images.ImageColumns.BUCKET_DISPLAY_NAME ); do { _id = cur.getInt(_idColumn); data = cur.getString(data_idColcum); dirName = cur.getString(dispalyname_Column); HashMap<String, String> map = new HashMap<>(); map.put(CommonConst.ImageMap. ID, _id + ""); map.put(CommonConst.ImageMap. PATH, data); map.put(CommonConst.ImageMap. DIR_NAME, dirName); // 图片所在文件夹路径 String imgDirPath = data.substring(0, data.lastIndexOf("/" )); if (!ImageCache.imagesIndex .containsKey(imgDirPath)) { // 当找不到 该图片所在文件夹路径 时 则创建一个新的List 并且注入到 Map 中 List<Map<String, String>> list = new ArrayList<Map<String, String>>(); list.add(map); ImageCache. imagesIndex.put(imgDirPath, list); Log. i(TAG, "img dir path = " + imgDirPath); } else { // 找到的话 在原有的List 添加该图片信息 ImageCache. imagesIndex.get(imgDirPath).add(map); } // 给图片所在文件夹路径 List 添加值 便于 计算出 adapter 使用 if (!ImageCache.imgDirPathList .contains(imgDirPath)) { ImageCache. imgDirPathList.add(imgDirPath); } } while (cur.moveToNext()); } initImagesThumb(); } /** * 初始化 读取 缩略图信息 */ public void initImagesThumb() { ContentResolver cr = mContext.getContentResolver(); String[] imagesProjection = { Thumbnails. IMAGE_ID, Thumbnails.DATA }; Cursor cur = cr.query(Thumbnails. EXTERNAL_CONTENT_URI, imagesProjection, null , null, null); if (cur.moveToFirst()) { String imagID; // 图片Path 具体路径 String imgThumbPath; // 图片所在目录的名称 int imagID_column = cur.getColumnIndex(Thumbnails.IMAGE_ID ); int imgThumbPath_column = cur.getColumnIndex(Thumbnails.DATA ); do { imagID = cur.getString(imagID_column); imgThumbPath = cur.getString(imgThumbPath_column); ImageCache. imgThumbMap.put(imagID, imgThumbPath); } while (cur.moveToNext()); } }