Two ways to get picture thumbnails on Android

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.