In the Android application development often uses the call system camera to take pictures and obtains the local picture function, below is the simple realization of this common function demo!
These two permissions are added to the capture picture feature.
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name= "Android.permission.CAMERA"/>
Layout file
1 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 Android:paddingbottom= "@dimen/activity_vertical_margin"6 Android:paddingleft= "@dimen/activity_horizontal_margin"7 Android:paddingright= "@dimen/activity_horizontal_margin"8 Android:paddingtop= "@dimen/activity_vertical_margin"9 Tools:context= "Com.hhkb.obtainmobilephoto.MainActivity" >Ten One <Button A Android:id= "@+id/btn_album" - Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" the Android:text= "@string/album" /> - - <Button - Android:id= "@+id/btn_camera" + Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" + Android:layout_torightof= "@id/btn_album" A Android:text= "@string/camera" /> at - <ImageView - Android:id= "@+id/iv_photo" - Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" - Android:layout_below= "@id/btn_camera" in android:src= "@drawable/pretty" /> - to </Relativelayout>
View Code
Code
1 Public class Mainactivity extends Activity implements Onclicklistener {2 3 private static final int CAMERA = 1025;4 private static final int ALBUM = 1026;5 6 TextView mbtnalbum, Mbtncamera;7 ImageView Mivphoto;8 9 private File Mfolder;Ten private String mimgname; One A @Override - protected void OnCreate (Bundle savedinstancestate) { - super.oncreate (savedinstancestate); the Initview (); - InitData (); - } - + private void Initview () { - Setcontentview (r.layout.activity_main); + mbtnalbum = (TextView) Findviewbyid (r.id.btn_album); A Mbtncamera = (TextView) Findviewbyid (R.id.btn_camera); at Mivphoto = (ImageView) Findviewbyid (R.id.iv_photo); - - } - - private void InitData () { - Mbtnalbum.setonclicklistener (this); in Mbtncamera.setonclicklistener (this); - } to + /* - * Set to get pictures from camera the */ * private void Getimgfromcamra () { $ String state = Environment.getexternalstoragestate ();Panax Notoginseng //First detect if there is a memory card. - if (state.equals (environment.media_mounted)) { the Mfolder = new File (Environment.getexternalstoragedirectory (), "BCache"); + //Determine if there is no this folder in the phone, no new. A if (!mfolder.exists ()) { the mfolder.mkdirs (); + } - //Custom image name, here is the number of milliseconds as the picture name. $ mimgname = System.currenttimemillis () + ". jpg"; $ uri uri = uri.fromfile (New File (Mfolder, Mimgname)); - //Call system photo function. - Intent Intent = new Intent (Android.provider.MediaStore.ACTION_IMAGE_CAPTURE); the Intent.putextra (Mediastore.extra_output, URI); - Startactivityforresult (Intent, CAMERA);Wuyi } else { the Toast.maketext (this, "SD card not detected", Toast.length_short). Show (); - } Wu } - About /* $ * Set to get pictures from local albums - */ - private void Getimgfromalbum () { - //Call this map library. A Intent Intent = new Intent (intent.action_get_content); + Intent.settype ("image/*"); the Startactivityforresult (Intent, ALBUM); - } $ the Public void Onactivityresult (int requestcode, int resultcode, Intent data) { the Bitmap BM; the if (ResultCode = = Result_ok && Requestcode = = CAMERA) { the //Call the system method to get the compressed picture, easily get the original picture through a custom path. - BM = Bitmapfactory.decodefile (Mfolder.getabsolutepath () in + file.separator + mimgname); the Mivphoto.setimagebitmap (BM); the } About the if (ResultCode = = Result_ok && Requestcode = = ALBUM) { the try { the if (data! = NULL) { + //Get a picture of your local album. - uri uri = Data.getdata (); the contentresolver cr = Getcontentresolver ();Bayi BM = Bitmapfactory.decodestream (Cr.openinputstream (URI)); the Mivphoto.setimagebitmap (BM); the } - } catch (FileNotFoundException e) { - e.printstacktrace (); the } the } the } the - @Override the Public void OnClick (View v) { the switch (V.getid ()) { the Case R.id.btn_album:94 getimgfromalbum (); the Break ; the Case R.id.btn_camera: the Getimgfromcamra ();98 Break ; About Default: - Break ;101 }102 103 };104}View Code
Android get photo pictures, local pictures simple implementation!