Android Development Step by step 66:android picture selection

Source: Internet
Author: User

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

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.