Package Com.xiaogang.imageheader; Import Java.io.File; Import Android.net.Uri; Import Android.os.Bundle; Import android.os.Environment; Import Android.provider.MediaStore; Import android.app.Activity; Import android.content.Intent; Import Android.graphics.Bitmap; Import android.graphics.drawable.BitmapDrawable; Import android.graphics.drawable.Drawable; Import Android.view.View; Import Android.view.View.OnClickListener; Import Android.widget.Button; Import Android.widget.ImageView; Import Android.widget.Toast; Public classMainactivity extends Activity implements Onclicklistener {Private StaticFinalintImage_request_code =0; Private StaticFinalintCamera_request_code =1; Private StaticFinalintResize_request_code =2; Private StaticFinal String Image_file_name ="header.jpg"; PrivateImageView Mimageheader; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Setupviews (); } Private voidsetupviews () {Mimageheader=(ImageView) Findviewbyid (R.id.image_header); Final Button selectBtn1=(Button) Findviewbyid (r.id.btn_selectimage); Final Button selectBtn2=(Button) Findviewbyid (R.id.btn_takephoto); Selectbtn1.setonclicklistener ( This); Selectbtn2.setonclicklistener ( This); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { Caser.id.btn_selectimage:intent galleryintent=NewIntent (intent.action_get_content); Galleryintent.addcategory (intent.category_openable); Galleryintent.settype ("image/*"); Startactivityforresult (Galleryintent, Image_request_code); Break; CaseR.id.btn_takephoto:if(Issdcardexisting ()) {Intent cameraintent=NewIntent ("Android.media.action.IMAGE_CAPTURE"); Cameraintent.putextra (Mediastore.extra_output, Getimageuri ()); Cameraintent.putextra (mediastore.extra_video_quality,0); Startactivityforresult (Cameraintent, Camera_request_code); } Else{Toast.maketext (V.getcontext (),"Please insert the SD card", Toast.length_long). Show (); } Break; }} @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { if(ResultCode! =RESULT_OK) { return; } Else { Switch(requestcode) { CaseImage_request_code:resizeimage (Data.getdata ()); Break; CaseCamera_request_code:if(Issdcardexisting ()) {Resizeimage (Getimageuri ()); } Else{toast.maketext (mainactivity). This,"No memory card found, no photos saved! ", Toast.length_long). Show (); } Break; CaseResize_request_code:if(Data! =NULL) {showresizeimage (data); } Break; }} super.onactivityresult (Requestcode, ResultCode, data); } PrivateBoolean issdcardexisting () {Final String state=environment.getexternalstoragestate (); if(State.equals (environment.media_mounted)) {return true; } Else { return false; } } Public voidresizeimage (Uri uri) {Intent Intent=NewIntent ("Com.android.camera.action.CROP"); Intent.setdataandtype (URI,"image/*"); Intent.putextra ("Crop","true"); Intent.putextra ("Aspectx",1); Intent.putextra ("aspecty",1); Intent.putextra ("OUTPUTX", Max); Intent.putextra ("outputy", Max); Intent.putextra ("Return-data",true); Startactivityforresult (Intent, Resize_request_code); } Private voidshowresizeimage (Intent data) {Bundle Extras=Data.getextras (); if(Extras! =NULL) {Bitmap photo= Extras.getparcelable ("Data"); Drawable drawable=Newbitmapdrawable (photo); Mimageheader.setimagedrawable (drawable); } } PrivateUri Getimageuri () {returnUri.fromfile (NewFile (Environment.getexternalstoragedirectory (), image_file_name)); } }
Android pick up albums and photo pictures cut get pictures