Directly go to the code. The comments in the Code are very clear. I think Google should have integrated this item, and it is best to call a method directly, because it is used more often, this is basically required for Communication Software on Android phones.
Package COM. example. photodemo; import android. app. activity; import android. content. contentresolver; import android. content. intent; import android. database. cursor; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android.net. uri; import android. OS. bundle; import android. OS. environment; import android. provider. mediastore; import android. view. view; import android. view. view. onclicklist Ener; import android. widget. button; import android. widget. imageview; import android. widget. toast; public class mainactivity extends activity {private imageview image; // Private bitmap photo for the selected image to be displayed; // The bitmap form of the selected image is private button button1; // picture private button button2; // picture Private Static final int PIC = 1; // picture Private Static final int Pho = 2; // photograph @ overrideprotected void oncreate (bundle savedinstancesta Te) {super. oncreate (savedinstancestate); super. setcontentview (R. layout. activity_main); button1 = (button) findviewbyid (R. id. button1); button2 = (button) findviewbyid (R. id. button2); image = (imageview) findviewbyid (R. id. imageview1); button1.setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {intent = new intent (); intent. settype ("image/*"); intent. setaction (intent. action _ Get_content); startactivityforresult (intent, PIC) ;}}); // upload a photo to the button2.setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {string state = environment. getexternalstoragestate (); If (state. equals (environment. media_mounted) {// if the memory card is available, intent = new intent ("android. media. action. image_capture "); startactivityforresult (intent, pho);} else {toast. maketext (getapplicationcont EXT (), "memory card unavailable", toast. length_long ). show () ;}}) ;}/ *** get the returned data */protected void onactivityresult (INT requestcode, int resultcode, intent data) {If (Data! = NULL & requestcode = Pho) {// bundle extras = data. getextras (); If (extras! = NULL) {photo = (Bitmap) extras. get ("data"); image. setimagebitmap (photo);} else {toast. maketext (mainactivity. this, "no image found", toast. length_long ). show () ;}} if (resultcode = activity. result_ OK) {URI uri = data. getdata (); // when the selected image is not empty, try {string [] pojo = {mediastore. images. media. data}; cursor = managedquery (Uri, pojo, null); If (cursor! = NULL) {contentresolver Cr = This. getcontentresolver (); int colunm_index = cursor. getcolumnindexorthrow (mediastore. images. media. data); cursor. movetofirst (); string Path = cursor. getstring (colunm_index);/***** this judgment is mainly used for third-party software selection. For example, if you use a third-party file manager, the file you selected is not necessarily an image. * In this case, we can determine the file suffix. If it is an image format, then we can */If (path. endswith ("jpg") | path. endswith ("PNG") {photo = bitmapfactory. decodestream (Cr. openinputstream (URI); image. setimagebitmap (photo);} else {}} catch (exception e ){}}}}
Let's also write down the layout.
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Tools = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: Orientation = "vertical" tools: context = ". mainactivity "> <button Android: Id =" @ + ID/button1 "Android: layout_width =" wrap_content "Android: layout_height =" wrap_content "Android: TEXT = "select photo"/> <button Android: Id = "@ + ID/button2" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = ""/> <edittext Android: Id = "@ + ID/edittext1" Android: layout_width = "match_parent" Android: layout_height = "150dp" Android: EMS = "10" Android: inputtype = "textmultiline"> <requestfocus/> </edittext> <imageview Android: Id = "@ + ID/imageview1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: src = "@ drawable/ic_launcher"/> </linearlayout>