I recently wrote a project and encountered it. Record it here ....
</P> <p> Import Java. io. bytearrayoutputstream; <br/> Import Java. io. inputstream; <br/> Import android. app. activity; <br/> Import android. app. alertdialog; <br/> Import android. content. contentresolver; <br/> Import android. content. dialoginterface; <br/> Import android. content. intent; <br/> Import android. database. cursor; <br/> Import android. graphics. bitmap; <br/> Import android. graphics. bitmapfactory; <br/> imp ORT android.net. uri; <br/> Import android. OS. bundle; <br/> Import android. provider. mediastore; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. imageview; </P> <p> public class picchooseractivity extends activity {<br/>/** called when the activity is first created. */<br/> private imageview; <br/> private onclicklistener imgviewlistener; <Br/> private bitmap mybitmap; <br/> private byte [] mcontent; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> imageview = (imageview) findviewbyid (R. id. ivpic); </P> <p> imageview. setonclicklistener (New onclicklistener () {</P> <p> @ override <br/> Public void onclick (view v) {<br/> // todo auto-gen Erated method stub <br/> final charsequence [] items = {"album", "photo" };</P> <p> alertdialog DLG = new alertdialog. builder (picchooseractivity. this ). settitle ("select photo "). setitems (items, <br/> New dialoginterface. onclicklistener () {</P> <p> @ override <br/> Public void onclick (dialoginterface dialog, int which) {<br/> // todo auto-generated method stub <br/> // here, item is based on the selected method. Two methods are defined in the items array, the subscript of the image is 1, so you can call the photo method. <br/> If (which = 1) {<br/> intent getimagebycamera = new intent ("android. media. action. image_capture "); <br/> startactivityforresult (getimagebycamera, 1); <br/>} else {<br/> intent getimage = new intent (intent. action_get_content); <br/> getimage. addcategory (intent. category_openable); <br/> getimage. settype ("image/JPEG"); <br/> startactivityforresult (getimage, 0); <br/>}</P> <p >}< br/> }). create (); <br/> DLG. show (); <Br/>}< br/>}); </P> <p >}</P> <p> @ override <br/> protected void onactivityresult (INT requestcode, int resultcode, intent data) {<br/> // todo auto-generated method stub <br/> super. onactivityresult (requestcode, resultcode, data); </P> <p> contentresolver = getcontentresolver (); <br/>/** <br/> * because the startactivityforresult method is used in both methods, the onactivityresult method is executed after the method is executed, <br/> * select the party for the difference The requestcode corresponds to the second parameter in startactivityforresult. <br/> */</P> <p> If (requestcode = 0) {</P> <p> // method 1 <br/>/* try {<br/> // retrieve the image URI <br/> URI orginaluri = data. getdata (); <br/> // parses the image content into a byte array <br/> mcontent = readstream (contentresolver. openinputstream (URI. parse (orginaluri. tostring (); <br/> // converts a byte array to a bitmap object called by imageview. <br/> mybitmap = getpicfrombytes (mcontent, null ); <br/> /// bind the image to the Controller. <Br/> imageview. setimagebitmap (mybitmap); <br/>} catch (exception e) {<br/> E. printstacktrace (); <br/> // todo: handle exception <br/>} */</P> <p> // method 2 <br/> try {<br/> URI selectedimage = data. getdata (); <br/> string [] filepathcolumn = {mediastore. images. media. data };</P> <p> cursor = getcontentresolver (). query (selectedimage, <br/> filepathcolumn, null); <br/> cursor. moveTo First (); </P> <p> int columnindex = cursor. getcolumnindex (filepathcolumn [0]); <br/> string picturepath = cursor. getstring (columnindex); <br/> cursor. close (); <br/> imageview. setimagebitmap (bitmapfactory. decodefile (picturepath); <br/>}catch (exception e) {<br/> // todo: handle exception <br/> E. printstacktrace (); <br/>}</P> <p>} else if (requestcode = 1) {<br/> try {<br/> bundle extras = data. getextra S (); <br/> mybitmap = (Bitmap) extras. get ("data"); <br/> bytearrayoutputstream baos = new bytearrayoutputstream (); <br/> mybitmap. compress (bitmap. compressformat. JPEG, 100, baos); <br/> mcontent = baos. tobytearray (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/> // todo: handle exception <br/>}< br/> imageview. setimagebitmap (mybitmap); <br/>}</P> <p> Public static bitmap getpic Frombytes (byte [] bytes, bitmapfactory. Options opts) {<br/> If (Bytes! = NULL) <br/> If (OPTs! = NULL) <br/> return bitmapfactory. decodebytearray (bytes, 0, bytes. length, opts); <br/> else <br/> return bitmapfactory. decodebytearray (bytes, 0, bytes. length); <br/> return NULL; <br/>}</P> <p> Public static byte [] readstream (inputstream in) throws exception {<br/> byte [] buffer = new byte [1024]; <br/> int Len =-1; <br/> bytearrayoutputstream outstream = new bytearrayoutputstream (); </P> <p> while (L En = in. Read (buffer ))! =-1) {<br/> outstream. write (buffer, 0, Len); <br/>}< br/> byte [] DATA = outstream. tobytearray (); <br/> outstream. close (); <br/> in. close (); <br/> return data; <br/>}</P> <p>}