Three.xml:
<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_width="match_parent"Android:layout_height="match_parent"android:orientation="Vertical"> <ImageView Android:id="@+id/imageview"Android:layout_width="wrap_content"Android:layout_height="wrap_content"android:src="@drawable/ic_launcher"/> <Button Android:id="@+id/buttonlocal"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="Local Albums Select Avatar"/> <Button Android:id="@+id/buttoncamera"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="mobile phone Photo select Avatar"/></linearlayout>
Threeactivity.class:
Package Com.example.day4_three;import Java.io.file;import android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.imageview;import Android.widget.toast;import Android.app.activity;import Android.content.intent;import Android.graphics.Bitmap; Import Android.net.uri;import android.os.bundle;import android.os.environment;import android.provider.MediaStore; Public classThreeactivity extends Activity {/*Avatar File*/ Private StaticFinal String Image_file_name ="temp_head_image.jpg"; /*Request Identification Code*/ Private StaticFinalintCode_gallery_request =0xa0; Private StaticFinalintCode_camera_request =0xa1; Private StaticFinalintCode_result_request =0xa2; //The width (X) and height (Y) of the picture after cropping, 480 x 480 square. (generating bitmap seems to be sometimes an error?) Try to make the size smaller. Private Static intoutput_x =480; Private Static intOutput_y =480; PrivateImageView Headimage =NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.three); Headimage=(ImageView) Findviewbyid (R.id.imageview); Button buttonlocal=(Button) Findviewbyid (r.id.buttonlocal); Buttonlocal.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {choseheadimagefromgallery (); } }); Button Buttoncamera=(Button) Findviewbyid (R.id.buttoncamera); Buttoncamera.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {choseheadimagefromcameracapture (); } }); } //Choose a picture from your local album as your avatar Private voidChoseheadimagefromgallery () {Intent intentfromgallery=NewIntent (); //Set File typeIntentfromgallery.settype ("image/*"); Intentfromgallery.setaction (intent.action_get_content); Startactivityforresult (Intentfromgallery, code_gallery_request); } //Start your phone camera and take a photo as your avatar Private voidchoseheadimagefromcameracapture () {Intent intentfromcapture=NewIntent (mediastore.action_image_capture); //determine if a memory card is available, store photo files if(Hassdcard ()) {Intentfromcapture.putextra (Mediastore.extra_output, Uri. FromFile (NewFile (Environment. getExternalStorageDirectory (), image_file_name))); } startactivityforresult (Intentfromcapture, code_camera_request); } @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent Intent) { //The user does not perform a valid setup operation and returns if(ResultCode = =result_canceled) {Toast.maketext (Getapplication (),"Cancel", Toast.length_long). Show (); return; } Switch(requestcode) { CaseCode_gallery_request:croprawphoto (Intent.getdata ()); Break; Casecode_camera_request:if(Hassdcard ()) {File tempfile=NewFile (Environment.getexternalstoragedirectory (), image_file_name); Croprawphoto (Uri.fromfile (tempfile)); } Else{Toast.maketext (Getapplication (),"no sdcard! .", Toast.length_long). Show (); } Break; Casecode_result_request:if(Intent! =NULL) {Setimagetoheadview (intent); } Break; } super.onactivityresult (Requestcode, ResultCode, intent); } /** * Crop the original picture*/ Public voidCroprawphoto (Uri uri) {Intent Intent=NewIntent ("Com.android.camera.action.CROP"); Intent.setdataandtype (URI,"image/*"); //Set CropIntent.putextra ("Crop","true"); //Aspectx, Aspecty: ratio of wide to highIntent.putextra ("Aspectx",1); Intent.putextra ("aspecty",1); //outputx, Outputy: Crop picture width heightIntent.putextra ("OUTPUTX", output_x); Intent.putextra ("outputy", output_y); Intent.putextra ("Return-data",true); Startactivityforresult (Intent, code_result_request); } /** * Extract saved picture data after cropping, and set the view of the Avatar section*/ Private voidSetimagetoheadview (Intent Intent) {Bundle Extras=Intent.getextras (); if(Extras! =NULL) {Bitmap photo= Extras.getparcelable ("Data"); Headimage.setimagebitmap (photo); } } /** * Tool method to check whether the device exists SDcard*/ Public StaticBoolean Hassdcard () {String State=environment.getexternalstoragestate (); if(State.equals (environment.media_mounted)) {//SDcard with Storage return true; } Else { return false; } }}
Android phone to take photos or select pictures inside the album