Set your avatar and call the picture editor in your phone

Source: Internet
Author: User

The following is a direct look at the main control class code 
  1.    ImportJava.io.File; Importandroid.app.Activity; ImportAndroid.app.AlertDialog; ImportAndroid.content.DialogInterface; Importandroid.content.Intent; ImportAndroid.graphics.Bitmap; Importandroid.graphics.drawable.BitmapDrawable; Importandroid.graphics.drawable.Drawable; ImportAndroid.net.Uri; ImportAndroid.os.Bundle; Importandroid.os.Environment; ImportAndroid.provider.MediaStore; ImportAndroid.view.View; ImportAndroid.view.View.OnClickListener; ImportAndroid.widget.Button; ImportAndroid.widget.ImageButton; ImportAndroid.widget.ImageView; /*** @Title: Piccutdemoactivity.java * @Package Com.xiaoma.piccut.demo * @Description: Picture clipping function Test *@authorXiaoma*/   Public classPiccutdemoactivityextendsActivityImplementsOnclicklistener {PrivateImageButton IB =NULL; PrivateImageView IV =NULL; PrivateButton BTN =NULL; PrivateString TP =NULL; /**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);          Setcontentview (R.layout.main); //Initializeinit (); }           /*** Initialization method implementation*/      Private voidinit () {IB=(ImageButton) Findviewbyid (R.id.imagebutton1); IV=(ImageView) Findviewbyid (R.ID.IMAGEVIEW1); BTN=(Button) Findviewbyid (R.id.button1); Ib.setonclicklistener ( This); Iv.setonclicklistener ( This); Btn.setonclicklistener ( This); }           /*** Control Click event Implementation * * Because a friend asked how the background of the different controls is cropped, * I've used three controls in this place, just for my own records * people feel useless can skip*/@Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.imagebutton1:showpickdialog ();  Break;  CaseR.id.imageview1:showpickdialog ();  Break;  CaseR.id.button1:showpickdialog ();  Break; default:               Break; }      }           /*** Select Prompt dialog box*/      Private voidShowpickdialog () {NewAlertdialog.builder ( This). Settitle ("Set Avatar ..."). Setnegativebutton ("Albums",NewDialoginterface.onclicklistener () { Public voidOnClick (Dialoginterface Dialog,intwhich)                          {Dialog.dismiss (); /*** At first, I do not know what Action_pick is doing, and then directly see intent Source code, * can find a lot of things, intent is a very strong Big things, everyone must read carefully under*/Intent Intent=NewIntent (Intent.action_pick,NULL); /*** The following sentence, written in other ways is the same effect, if: * Intent.setdata (MEDIASTORE.IMAGES.MEDIA.E                          Xternal_content_uri);                          * Intent.settype ("image/*"); Set data type * If your friends want to limit the types of images uploaded to the server, you can write them directly such as: "Image/jpeg, image/png, etc." * This place pony has a question, hope Master answer: Is this data URI and type why should be divided into two forms to write it?                          What's the difference? */Intent.setdataandtype (MediaStore.Images.Media.EXTERNAL_CON Tent_uri,"Image/*"); Startactivityforresult (Intent,1); }}). Setpositivebutton ("Take pictures",NewDialoginterface.onclicklistener () { Public voidOnClick (Dialoginterface Dialog,intWhichbutton)                          {Dialog.dismiss (); /*** The following sentence is still the same as the old, call the Quick camera function, as for why the quick photo, you can refer to the following official * document, You_sdk_path/docs                           /guide/topics/media/camera.html * I just looked at the time because too long to look seriously, in fact, it is wrong, this is too much useful, so we do not think * Official documents too long to see, in fact, is wrong, this place the pony is also wrong, must correct*/Intent Intent=NewIntent (mediastore.action_image_capture); //The following sentence specifies the path of the photo store after the camera is calledIntent.putextra (Mediastore.extra_output, Uri. FromFile (NewFile (Environment. getExternalStorageDirectory (), "Xiaoma.jpg"))); Startactivityforresult (Intent,2);      }}). Show (); } @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {          Switch(requestcode) {//If you get it directly from the album         Case1: Startphotozoom (Data.getdata ());  Break; //If you are calling the camera to take a picture         Case2: File Temp=NewFile (environment.getexternalstoragedirectory ()+ "/xiaoma.jpg");              Startphotozoom (Uri.fromfile (temp));  Break; //get the cropped picture         Case3:              /*** Non-null judgment everyone must verify, if not verified, * after clipping if found not satisfied, to re-crop, discard * Current function, will be reported nullexception, pony only * In this place, you can make judgments in the right places according to different situations **/              if(Data! =NULL) {Setpictoview (data); }               Break; default:               Break; }          Super. Onactivityresult (Requestcode, ResultCode, data); }           /*** Crop Image method Implementation *@paramURI*/       Public voidstartphotozoom (Uri uri) {/** As for the following intent action is how to know, everyone can look under their path under the following page * Yourself_sdk_path/docs/reference/android/content/int ent.html * directly in the inside Ctrl+f search: CROP, before the pony did not carefully read, in fact, the Android system already has a picture clipping function, * is directly local library, pony do not understand C C + + This does not do a detailed understanding of the wheel, with wheels, no longer research How the wheel is made ... Ho Ho*/Intent Intent=NewIntent ("Com.android.camera.action.CROP"); Intent.setdataandtype (URI,"Image/*"); //The following crop=true is set to set the display in the open intent view can be croppedIntent.putextra ("Crop", "true"); //Aspectx Aspecty is the ratio of width to heightIntent.putextra ("Aspectx", 1); Intent.putextra ("Aspecty", 1); //Outputx Outputy is the cropped picture width highIntent.putextra ("Outputx", 150); Intent.putextra ("Outputy", 150); Intent.putextra ("Return-data",true); Startactivityforresult (Intent,3); }           /*** Save image data after cropping *@paramPicdata*/      Private voidSetpictoview (Intent picdata) {Bundle Extras=Picdata.getextras (); if(Extras! =NULL) {Bitmap photo= extras.getparcelable ("Data"); Drawable drawable=Newbitmapdrawable (photo); /*** The following comment method is to be cropped after the picture in Base64coder characters * to the server, QQ avatar upload method similar to this*/                  /*Bytearrayoutputstream stream = new Bytearrayoutputstream ();             Photo.compress (Bitmap.CompressFormat.JPEG, Max, stream);             Byte[] B = Stream.tobytearray ();             Store the picture stream as a string tp = new String (Base64coder.encodelines (b)); /** This place everyone can write down to the server upload image implementation, directly to the TP upload directly can, * Server processing method is the server side of the matter, roar * if the data downloaded to the server is in the form of Base64coder , can be converted in the following way * for we can use the type of picture ok ... Ho Ho*/Bitmap Dbitmap=bitmapfactory.decodefile (TP); Drawable drawable=Newbitmapdrawable (DBITMAP); */ib.setbackgrounddrawable (drawable);          Iv.setbackgrounddrawable (drawable); }      }       }  

Finally, the small demo source this download: http://mzh3344258.blog.51cto.com/blog/1823534/808837

Set your avatar and call the picture editor in your phone

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.