Android系统能够帮助我们实现把图片缩略到合适的尺寸:
提取图片和视频的所有缩略图可以直接访问:
android.provider.MediaStroe.Imamge.Thumbnails
android.provider.MediaStroe.Imamge.Thumbnails
这是两个系统提供的数据库,可以用来查询缩略图(contentprovider)。
想要知道这两个数据库是如何存储数据的,可以参见这篇博文:http://blog.csdn.net/java2009cgh/article/details/8364735。
我这里只上一下自己写的代码,用来提取android系统的所有缩略图:
首先建立一个类,声明要用到的一些类和变量
final String TAG = getClass().getSimpleName(); Context context; ContentResolver cr; // 缩略图列表 HashMap<String, String> thumbnailList = new HashMap<String, String>(); // 专辑列表 List<HashMap<String, String>> albumList = new ArrayList<HashMap<String, String>>(); HashMap<String, PhotoUpImageBucket> bucketList = new HashMap<String, PhotoUpImageBucket>(); private GetAlbumList getAlbumList; /** * 是否创建了图片集 */ boolean hasBuildImagesBucketList = false; private PhotoUpAlbumHelper() { } public static PhotoUpAlbumHelper getHelper() { PhotoUpAlbumHelper instance = new PhotoUpAlbumHelper(); return instance; } /** * 初始化 * * @param context */ public void init(Context context) { if (this.context == null) { this.context = context; cr = context.getContentResolver(); } }
其中PhotoUpImageBucket是一个相册对象,如下:
package android.apps.bean; import java.io.Serializable; import java.util.List; /** * 一个目录下的相册对象 */ @SuppressWarnings("serial") public class PhotoUpImageBucket implements Serializable{ public int count = 0; public String bucketName; public List<PhotoUpImageItem> imageList; public int getCount() { return count; } public void setCount(int count) { this.count = count; } public String getBucketName() { return bucketName; } public void setBucketName(String bucketName) { this.bucketName = bucketName; } public List<PhotoUpImageItem> getImageList() { return imageList; } public void setImageList(List<PhotoUpImageItem> imageList) { this.imageList = imageList; } }
相册对象中有一个PhotoUpImageItem类,是缩略图对象,其中的属性如下:
package android.apps.bean; import java.io.Serializable; @SuppressWarnings("serial") public class PhotoUpImageItem implements Serializable { //图片ID private String imageId; //原图路径 private String imagePath; //是否被选择 private boolean isSelected = false; public String getImageId() { return imageId; } public void setImageId(String imageId) { this.imageId = imageId; } public String getImagePath() { return imagePath; } public void setImagePath(String imagePath) { this.imagePath = imagePath; } public boolean isSelected() { return isSelected; } public void setSelected(boolean isSelected) { this.isSelected = isSelected; } }
随后
1.得到系统的缩略图:
/** * 得到缩略图 */ private void getThumbnail() { String[] projection = { Thumbnails._ID, Thumbnails.IMAGE_ID, Thumbnails.DATA }; Cursor cursor1 = Thumbnails.queryMiniThumbnails(cr, Thumbnails.EXTERNAL_CONTENT_URI, Thumbnails.MINI_KIND, projection); getThumbnailColumnData(cursor1); cursor1.close(); } /** * 从数据库中得到缩略图 * * @param cur */ private void getThumbnailColumnData(Cursor cur) { if (cur.moveToFirst()) { int image_id; String image_path; int image_idColumn = cur.getColumnIndex(Thumbnails.IMAGE_ID); int dataColumn = cur.getColumnIndex(Thumbnails.DATA); do { image_id = cur.getInt(image_idColumn); image_path = cur.getString(dataColumn); thumbnailList.put("" + image_id, image_path); } while (cur.moveToNext()); } }
2.得到缩略图对应的原图:
/** * 得到原图-n */ void getAlbum() { String[] projection = { Albums._ID, Albums.ALBUM, Albums.ALBUM_ART, Albums.ALBUM_KEY, Albums.ARTIST, Albums.NUMBER_OF_SONGS }; Cursor cursor1 = cr.query(Albums.EXTERNAL_CONTENT_URI, projection, null, null, Albums.ALBUM_ID + " desc"); getAlbumColumnData(cursor1); cursor1.close(); } /** * 从本地数据库中得到原图 * * @param cur */ 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); 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()); } }
3.最后编写一个方法来构建图片集合
public void buildImagesBucketList() { // 构造缩略图索引 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, Media.DATE_MODIFIED + " desc"); if (cur.moveToFirst()) { // 获取指定列的索引 int photoIDIndex = cur.getColumnIndexOrThrow(Media._ID); int photoPathIndex = cur.getColumnIndexOrThrow(Media.DATA); int bucketDisplayNameIndex = cur .getColumnIndexOrThrow(Media.BUCKET_DISPLAY_NAME); int bucketIdIndex = cur.getColumnIndexOrThrow(Media.BUCKET_ID); /** * Description:这里增加了一个判断:判断照片的名字是否合法,例如.jpg .png 图片名字是不合法的,直接过滤掉 */ do { if (cur.getString(photoPathIndex) .substring( cur.getString(photoPathIndex).lastIndexOf("/") + 1, cur.getString(photoPathIndex).lastIndexOf(".")) .replaceAll(" ", "").length() <= 0) { Log.d(TAG, "出现了异常图片的地址:cur.getString(photoPathIndex)=" + cur.getString(photoPathIndex)); Log.d(TAG, "出现了异常图片的地址:cur.getString(photoPathIndex).substring=" + cur.getString(photoPathIndex).substring( cur.getString(photoPathIndex) .lastIndexOf("/") + 1, cur.getString(photoPathIndex) .lastIndexOf("."))); } else { String _id = cur.getString(photoIDIndex); String path = cur.getString(photoPathIndex); String bucketName = cur.getString(bucketDisplayNameIndex); String bucketId = cur.getString(bucketIdIndex); PhotoUpImageBucket bucket = bucketList.get(bucketId); if (bucket == null) { bucket = new PhotoUpImageBucket(); bucketList.put(bucketId, bucket); bucket.imageList = new ArrayList<PhotoUpImageItem>(); bucket.bucketName = bucketName; } bucket.count++; PhotoUpImageItem imageItem = new PhotoUpImageItem(); imageItem.setImageId(_id); imageItem.setImagePath(path); // imageItem.setThumbnailPath(thumbnailList.get(_id)); bucket.imageList.add(imageItem); Log.i(TAG, "PhotoUpAlbumHelper类中 的——》path=" + thumbnailList.get(_id)); } } while (cur.moveToNext()); } cur.close(); hasBuildImagesBucketList = true; }
4.得到图片集合:
/** * 得到图片集 * * @param refresh * @return */ public List<PhotoUpImageBucket> getImagesBucketList(boolean refresh) { if (refresh || (!refresh && !hasBuildImagesBucketList)) { buildImagesBucketList(); } List<PhotoUpImageBucket> tmpList = new ArrayList<PhotoUpImageBucket>(); Iterator<Entry<String, PhotoUpImageBucket>> itr = bucketList.entrySet() .iterator(); while (itr.hasNext()) { Map.Entry<String, PhotoUpImageBucket> entry = (Map.Entry<String, PhotoUpImageBucket>) itr .next(); tmpList.add(entry.getValue()); } return tmpList; }
最后提供一个获取图片路径的方法:
/** * 得到原始图像路径 * * @param image_id * @return */ String getOriginalImagePath(String image_id) { String path = null; Log.i(TAG, "--" + image_id); String[] projection = { Media._ID, Media.DATA }; Cursor cursor = cr .query(Media.EXTERNAL_CONTENT_URI, projection, Media._ID + "=" + image_id, null, Media.DATE_MODIFIED + " desc"); if (cursor != null) { cursor.moveToFirst(); path = cursor.getString(cursor.getColumnIndex(Media.DATA)); } return path; }
一般像这种获取图片的类都要使用异步的方式获取,在其中使用异步回调机制来获取图片集合。
有问题的可以随时问我。。。先写这么多