Touch Android for so long. There is no real vast look at the Android photos, album Pictures cut in the end is how it, each time from someone else's code a button, came over. In fact, Google provides the API is already very powerful. Just need to use the good, just a few words will be able to deal with the mysterious photos and cut pictures. Not much nonsense, read the source directly
PackageCom.example.testcamera;Importjava.io.FileNotFoundException;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.provider.MediaStore;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView; Public classMainactivityextendsActivityImplementsOnclicklistener {PrivateButton BTNC, BTNP; PrivateImageView img; PrivateIntent Intent; Private Final intDatafromcamera = 1; Private Final intDatafromalbum = 2; Private Final intCropfromcamera = 3; Private Final intCropfromalbum = 4; PrivateUri MUri =NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initview (); } Private voidInitview () {BTNC=(Button) Findviewbyid (R.ID.BTNC); BTNP=(Button) Findviewbyid (R.ID.BTNP); IMG=(ImageView) Findviewbyid (r.id.img); Btnc.setonclicklistener ( This); Btnp.setonclicklistener ( This); } @Override Public voidOnClick (View v) {intID =V.getid (); Switch(ID) { CaseR.id.btnc:opencamera (); Break; CaseR.id.btnp:openphoto (); Break; default: Break; } } Private voidOpencamera () {Intent=NewIntent (mediastore.action_image_capture); Startactivityforresult (Intent, Datafromcamera); } Private voidOpenphoto () {Intent=NewIntent (intent.action_get_content); Intent.settype ("Image/*"); Startactivityforresult (Intent, datafromalbum); } @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { Super. Onactivityresult (Requestcode, ResultCode, data); if(ResultCode = =RESULT_OK) {URI Uri=NULL; if(Data.getdata ()! =NULL) {URI=Data.getdata (); MUri=URI; } Switch(requestcode) { CaseDatafromcamera:cropimageuri (URI,800, 600, Cropfromcamera); Break; CaseDatafromalbum:cropimageuri (URI,800, 600, Cropfromalbum); Break; CaseCropfromcamera:if(MUri! =NULL) {Bitmap Bitmap=Decodeuriasbitmap (MUri); Img.setimagebitmap (bitmap); } Break; CaseCropfromalbum:if(MUri! =NULL) {Bitmap Bitmap=Decodeuriasbitmap (MUri); Img.setimagebitmap (bitmap); } Break; default: Break; } } } Private voidCropimageuri (Uri Uri,intOUTPUTX,intOutputy,intRequestcode) {Intent Intent=NewIntent ("Com.android.camera.action.CROP"); Intent.setdataandtype (URI,"Image/*"); Intent.putextra ("Crop", "true"); Intent.putextra ("Aspectx", 4); Intent.putextra ("Aspecty", 3); Intent.putextra ("Outputx", OUTPUTX); Intent.putextra ("Outputy", outputy); Intent.putextra ("Scale",true); Intent.putextra (Mediastore.extra_output, URI); Intent.putextra ("Return-data",false); Intent.putextra ("OutputFormat", Bitmap.CompressFormat.JPEG.toString ()); Intent.putextra ("Nofacedetection",true);//No Face DetectionStartactivityforresult (Intent, Requestcode); } PrivateBitmap decodeuriasbitmap (Uri uri) {Bitmap Bitmap=NULL; Try{bitmap=Bitmapfactory.decodestream (Getcontentresolver (). Openinputstream (URI)); } Catch(FileNotFoundException e) {e.printstacktrace (); return NULL; } returnbitmap; }}
XML layout file
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <ButtonAndroid:id= "@+id/btnc"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "@android: Color/holo_purple"Android:text= "Take pictures"Android:textcolor= "@android: Color/white" /> <ButtonAndroid:id= "@+id/btnp"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "@android: Color/holo_orange_dark"Android:text= "Choose from album"Android:textcolor= "@android: Color/white" /> <ImageViewAndroid:id= "@+id/img"Android:layout_width= "300DP"Android:layout_height= "300DP"android:layout_gravity= "Center_horizontal"Android:layout_margintop= "50DP" /></LinearLayout>
Mainfest.xml file
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.testcamera"Android:versioncode= "1"Android:versionname= "1.0" > <USES-SDKandroid:minsdkversion= "+"android:targetsdkversion= "+" /> <uses-permissionAndroid:name= "Android.permission.CAMERA" /> <uses-featureAndroid:name= "Android.hardware.camera" /> <uses-featureAndroid:name= "Android.hardware.camera.autofocus" /> <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <ActivityAndroid:name=". Mainactivity "Android:label= "@string/app_name" > <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> </Application></Manifest>
Note To configure permissions yes.
android--photos, album Pictures cut it's so simple