1.
/** * Using the content provider to get picture thumbnails disadvantages: You must update your media library to see the latest thumbnails * @param context * @param cr * @param Imagepath * @return */public Stati C Bitmap Getimagethumbnail (context context, Contentresolver CR, String Imagepath) {Contentresolver TESTCR = Co Ntext.getcontentresolver (); string[] projection = {MediaStore.Images.Media.DATA, mediastore.images.media._id,}; String Whereclause = MediaStore.Images.Media.DATA + "= '" + Imagepath + "'"; cursor cursor = testcr.query (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, whereclause,null, null); int _id = 0; String ImagePath = ""; if (cursor = = NULL | | Cursor.getcount () = = 0) {return null; }else if (Cursor.movetofirst ()) {int _idcolumn = Cursor.getcolumnindex (mediastore.images.media._id); int _datacolumn = Cursor.getcolumnindex (MediaStore.Images.Media.DATA); do { _id = Cursor.getint (_idcolumn); ImagePath = cursor.getstring (_datacolumn); } while (Cursor.movetonext ()); } cursor.close (); Bitmapfactory.options Options = new Bitmapfactory.options (); Options.indither = false; Options.inpreferredconfig = Bitmap.Config.RGB_565; Bitmap Bitmap = MediaStore.Images.Thumbnails.getThumbnail (CR, _id, images.thumbnails.mini_kind,options); return bitmap; }
2, manually with the specified wide-height acquisition
Cons: More Time consuming
private void Getimagethumbnail (String imagepath,int width,int height) {Bitmap Img_bitmap = null;//Save Memory Options.inpreferr Edconfig = bitmap.config.argb_4444;/* setting allows decoder to be decoded in the best way */options.inpurgeable = True;options.ininputshareable = True;o Ptions.injustdecodebounds = true; If Dither is true, the decoder would attempt to dither the decoded imageoptions.indither = false;//do not take picture jitter processing//Get the width and height of this picture , note that the bitmap here is null Img_bitmap = Bitmapfactory.decodefile (params[0], options); options.injustdecodebounds = false;//is set to False//calculates the scaling ratio int h = options.outheight; int w = options.outwidth; int bewidth = W/width; int beheight = H/height; int be = 1; if (Bewidth < beheight) {be = Bewidth; } else {be = Beheight; } if (be <= 0) {is = 1; }options.insamplesize = be; Re-read into the picture, read the scaled bitmap, note this time to set Options.injustdecodebounds to False img_bitmap = Bitmapfactory.decodefile (Params[0], Options); Use Thumbnailutils to create thumbnails, where you specify which bitmap object to scale Img_bitmap = Thumbnailutils.extractthumbnail (img_bitmap, width, height, thumbnailutils.options_recycle_input); return img_bitmap;}
Two ways to get picture thumbnails on Android