Recently made a page, feedback question page, there is a user upload problem picture function. Originally very stupid want to put all the pictures of the system, and then let the user choose, and later found that the original can directly open the phone all the pictures of the API. Effect
Give the main code:
1. Select picture
Intent i = new Intent (Intent.action_pick, Android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); Startactivityforresult (i, 1);
2. Get Pictures
 
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresult (REQUESTC Ode, ResultCode, data); try {if (Requestcode = = 1 && resultcode = = RESULT_OK && null! = data) {Uri sel Ectedimage = Data.getdata (); String[] Filepathcolumn = {MediaStore.Images.Media.DATA}; cursor cursor = getcontentresolver (). Query (selectedimage, filepathcolumn, NULL, NULL, NULL); Cursor.movetofirst (); int columnindex = Cursor.getcolumnindex (filepathcolumn[0]); String PicturePath = cursor.getstring (columnindex); Cursor.close (); Bitmapfactory.options opts = new Bitmapfactory.options (); Opts.injustdecodebounds = true; Bitmapfactory.decodefile (PicturePath, opts); Opts.insamplesize = Computesamplesize (opts,-1, 160 * 160);//Convert to 160*160 size picture//Make sure to set it back to FalsE, because before we set it to true opts.injustdecodebounds = false; try {Bitmap bmp = Bitmapfactory.decodefile (PicturePath, opts); if (mposition! = 0)//replace {Fba.getmimgs (). Set (mposition, BMP); Fba.getmimgpaths (). Set (Mposition, PicturePath); } else {Fba.getmimgs (). Add (BMP); Fba.getmimgpaths (). Add (PicturePath); }} catch (OutOfMemoryError err) {if (err! = null) {ERR.PRINTST Acktrace (); }} fba.notifydatasetchanged (); }} catch (Exception e) {if (E! = null) {e.printstacktrace (); }} catch (OutOfMemoryError e) {if (E! = null) {e.printstacktrace (); } } }
public int computesamplesize (bitmapfactory.options Options, int minsidelength, int maxnumofpixels) {int initial Size = computeinitialsamplesize (options, Minsidelength, maxnumofpixels); int roundedsize; if (initialsize <= 8) {roundedsize = 1; while (Roundedsize < initialsize) {roundedsize <<= 1; }} else {roundedsize = (initialsize + 7)/8 * 8; } return roundedsize; } private int Computeinitialsamplesize (bitmapfactory.options Options, int minsidelength, int maxnumofpixels) {D ouble w = options.outwidth; Double h = options.outheight; int lowerbound = (Maxnumofpixels = =-1)? 1: (int) Math.ceil (math.sqrt (w * h/maxnumofpixels)); int upperbound = (Minsidelength = =-1)? : (int) math.min (Math.floor (w/minsidelength), Math.floor (h/minsidelength)); if (Upperbound < lowerbound) {return lowerbound; } if ((Maxnumofpixels = =-1) && (minsidelength = =-1)) {return 1; } else if (minsidelength = =-1) {return lowerbound; } else {return upperbound; } }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Development Step by step 66:android picture selection