/** * 根据图片的路径得到该图片在表中的ID * @param cr * @param fileName * @return */ public static String getImageIdFromPath(ContentResolver cr, String fileName) { //select condition. String whereClause = MediaStore.Images.Media.DATA + " = ‘" + fileName + "‘"; //colection of results. Cursor cursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media._ID}, whereClause, null, null); if (cursor == null || cursor.getCount() == 0) { if (cursor != null) cursor.close(); return null; } cursor.moveToFirst(); //image id in image table. String imageId = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media._ID)); cursor.close(); if (imageId == null) { return null; } return imageId; } /** * 根据图片的ID得到缩略图 * @param cr * @param imageId * @return */ public static Bitmap getThumbnailsFromImageId(ContentResolver cr, String imageId) { if (imageId == null || imageId.equals("")) return null; Bitmap bitmap = null; BitmapFactory.Options options = new BitmapFactory.Options(); options.inDither = false; options.inPreferredConfig = Bitmap.Config.ARGB_8888; long imageIdLong = Long.parseLong(imageId); //via imageid get the bimap type thumbnail in thumbnail table. bitmap = MediaStore.Images.Thumbnails.getThumbnail(cr, imageIdLong, MediaStore.Images.Thumbnails.MINI_KIND, options); return bitmap; }
原文:http://www.cnblogs.com/zzw1994/p/5212362.html