1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
| public class AlbumHelper { private final String TAG = getClass().getSimpleName(); private Context context; private ContentResolver cr;
public HashMap<String, String> thumbnailList = new HashMap<String, String>(); private List<HashMap<String, String>> albumList = new ArrayList<HashMap<String, String>>(); private HashMap<String, ImageBucket> bucketList = new HashMap<String, ImageBucket>();
private static AlbumHelper instance;
private AlbumHelper() { }
public static AlbumHelper getHelper() { if (instance == null) { instance = new AlbumHelper(); } return instance; }
public void init(Context context) { if (this.context == null) { this.context = context; cr = context.getContentResolver(); } }
private void getThumbnail() { String[] projection = { Thumbnails._ID, Thumbnails.IMAGE_ID, Thumbnails.DATA }; Cursor cursor = cr.query(Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, null); getThumbnailColumnData(cursor); }
private void getThumbnailColumnData(Cursor cur) { if (cur.moveToFirst()) { int _id; int image_id; String image_path; int _idColumn = cur.getColumnIndex(Thumbnails._ID); int image_idColumn = cur.getColumnIndex(Thumbnails.IMAGE_ID); int dataColumn = cur.getColumnIndex(Thumbnails.DATA);
do { _id = cur.getInt(_idColumn); image_id = cur.getInt(image_idColumn); image_path = cur.getString(dataColumn); thumbnailList.put("" + image_id, image_path); } while (cur.moveToNext()); } }
private void getAlbum() { String[] projection = { Albums._ID, Albums.ALBUM, Albums.ALBUM_ART, Albums.ALBUM_KEY, Albums.ARTIST, Albums.NUMBER_OF_SONGS }; Cursor cursor = cr.query(Albums.EXTERNAL_CONTENT_URI, projection, null, null, null); getAlbumColumnData(cursor);
}
private void getAlbumColumnData(Cursor cur) { if (cur.moveToFirst()) { int _id; String album; String albumArt; String albumKey; String artist; int numOfSongs;
int _idColumn = cur.getColumnIndex(Albums._ID); int albumColumn = cur.getColumnIndex(Albums.ALBUM); int albumArtColumn = cur.getColumnIndex(Albums.ALBUM_ART); int albumKeyColumn = cur.getColumnIndex(Albums.ALBUM_KEY); int artistColumn = cur.getColumnIndex(Albums.ARTIST); int numOfSongsColumn = cur.getColumnIndex(Albums.NUMBER_OF_SONGS);
do { _id = cur.getInt(_idColumn); album = cur.getString(albumColumn); albumArt = cur.getString(albumArtColumn); albumKey = cur.getString(albumKeyColumn); artist = cur.getString(artistColumn); numOfSongs = cur.getInt(numOfSongsColumn);
Log.i(TAG, _id + " album:" + album + " albumArt:" + albumArt + "albumKey: " + albumKey + " artist: " + artist + " numOfSongs: " + numOfSongs + "---"); HashMap<String, String> hash = new HashMap<String, String>(); hash.put("_id", _id + ""); hash.put("album", album); hash.put("albumArt", albumArt); hash.put("albumKey", albumKey); hash.put("artist", artist); hash.put("numOfSongs", numOfSongs + ""); albumList.add(hash); } while (cur.moveToNext()); } }
private boolean hasBuildImagesBucketList = false;
private void buildImagesBucketList() { long startTime = System.currentTimeMillis();
getThumbnail();
String columns[] = new String[] { Media._ID, Media.BUCKET_ID, Media.PICASA_ID, Media.DATA, Media.DISPLAY_NAME, Media.TITLE, Media.SIZE, Media.BUCKET_DISPLAY_NAME }; Cursor cur = cr.query(Media.EXTERNAL_CONTENT_URI, columns, null, null, null); if (cur.moveToFirst()) { int photoIDIndex = cur.getColumnIndexOrThrow(Media._ID); int photoPathIndex = cur.getColumnIndexOrThrow(Media.DATA); int photoNameIndex = cur.getColumnIndexOrThrow(Media.DISPLAY_NAME); int photoTitleIndex = cur.getColumnIndexOrThrow(Media.TITLE); int photoSizeIndex = cur.getColumnIndexOrThrow(Media.SIZE); int bucketDisplayNameIndex = cur .getColumnIndexOrThrow(Media.BUCKET_DISPLAY_NAME); int bucketIdIndex = cur.getColumnIndexOrThrow(Media.BUCKET_ID); int picasaIdIndex = cur.getColumnIndexOrThrow(Media.PICASA_ID); int totalNum = cur.getCount();
do { String _id = cur.getString(photoIDIndex); String name = cur.getString(photoNameIndex); String path = cur.getString(photoPathIndex); String title = cur.getString(photoTitleIndex); String size = cur.getString(photoSizeIndex); String bucketName = cur.getString(bucketDisplayNameIndex); String bucketId = cur.getString(bucketIdIndex); String picasaId = cur.getString(picasaIdIndex);
Log.i(TAG, _id + ", bucketId: " + bucketId + ", picasaId: " + picasaId + " name:" + name + " path:" + path + " title: " + title + " size: " + size + " bucket: " + bucketName + "---");
ImageBucket bucket = bucketList.get(bucketId); if (bucket == null) { bucket = new ImageBucket(); bucketList.put(bucketId, bucket); bucket.imageList = new ArrayList<ImageItem>(); bucket.bucketName = bucketName; } bucket.count++; ImageItem imageItem = new ImageItem(); imageItem.imageId = _id; imageItem.imagePath = path; imageItem.thumbnailPath = thumbnailList.get(_id); bucket.imageList.add(imageItem);
} while (cur.moveToNext()); } Iterator<Entry<String, ImageBucket>> itr = bucketList.entrySet() .iterator(); while (itr.hasNext()) { Map.Entry<String, ImageBucket> entry = (Map.Entry<String, ImageBucket>) itr .next(); ImageBucket bucket = entry.getValue(); Log.d(TAG, entry.getKey() + ", " + bucket.bucketName + ", " + bucket.count + " ---------- "); for (int i = 0; i < bucket.imageList.size(); ++i) { ImageItem image = bucket.imageList.get(i); Log.d(TAG, "----- " + image.imageId + ", " + image.imagePath + ", " + image.thumbnailPath); } } hasBuildImagesBucketList = true; long endTime = System.currentTimeMillis(); Log.d(TAG, "use time: " + (endTime - startTime) + " ms"); }
public List<ImageBucket> getImagesBucketList(boolean refresh) { if (refresh || (!refresh && !hasBuildImagesBucketList)) { buildImagesBucketList(); } List<ImageBucket> tmpList = new ArrayList<ImageBucket>(); Iterator<Entry<String, ImageBucket>> itr = bucketList.entrySet() .iterator(); while (itr.hasNext()) { Map.Entry<String, ImageBucket> entry = (Map.Entry<String, ImageBucket>) itr .next(); tmpList.add(entry.getValue()); } return tmpList; }
public String getOriginalImagePath(String image_id) { String path = null; Log.i(TAG, "---(^o^)----" + image_id); String[] projection = { Media._ID, Media.DATA }; Cursor cursor = cr.query(Media.EXTERNAL_CONTENT_URI, projection, Media._ID + "=" + image_id, null, null); if (cursor != null) { cursor.moveToFirst(); path = cursor.getString(cursor.getColumnIndex(Media.DATA)); } return path; } }
|