Demo Source Location: Http://git.oschina.net/zj2012zy/Android-Demo/tree/master/AndroidDemo/headset
Generally need user information of a lot of also need to set up the user picture, usually set the user's avatar manipulation either from the phone to choose a picture, or directly from the phone to take pictures to set up. As follows:
In addition: the production of mobile phone dynamic screenshot, you can use the app Bao's dynamic screenshot function, very convenient.
The layout file is simple: It is two buttons plus a imageview to display the settings of the Avatar, do not say.
The core code is the following four functions:
1. Select a picture
1 Public Static void Selectphoto (activity activity) {2 New Intent (intent.action_get_content); 3 Intent.settype ("Image/jpeg"); 4 true ); 5 Activity.startactivityforresult (Intent, photo_picked_with_data); 6 }
2. Take photos and store
1 Public StaticString takepicture (activity activity) {2Createdirfile (Environment.getexternalstoragedirectory (). GetPath () + "/zhaojie/images/");3Intent Intent =NewIntent (mediastore.action_image_capture);4String TimeStamp =NewSimpleDateFormat ("Yyyymmddhhmmss"). Format (NewDate ());5String path = Environment.getexternalstoragedirectory (). GetPath () + "/zhaojie/images/" + TimeStamp + ". png";6File File =createnewfile (path);7 if(File! =NULL) {8 Intent.putextra (mediastore.extra_output, Uri.fromfile (file));9 }Ten Activity.startactivityforresult (Intent, intent_request_code_camera); One returnpath; A}
3, processing pictures or selected photos, capture the avatar, which can be specified to capture the size of the avatar.
1 Private voidCropimageuri (Uri Uri,intRequestcode) {2Intent Intent =NewIntent ("Com.android.camera.action.CROP");3Intent.setdataandtype (URI, "image/*");4Intent.putextra ("Crop", "true");5Intent.putextra ("Aspectx", 1);6Intent.putextra ("Aspecty", 1);7Intent.putextra ("Outputx", 320);8Intent.putextra ("Outputy", 320);9Intent.putextra ("scale",true);Ten Intent.putextra (Mediastore.extra_output, URI); OneIntent.putextra ("Return-data",true); AIntent.putextra ("OutputFormat", Bitmap.CompressFormat.JPEG.toString ()); -Intent.putextra ("Nofacedetection",true);//No Face Detection - Startactivityforresult (Intent, requestcode); the}
4, the overall process is basically through the call system activity implementation, the code is as follows
1 protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {2 Super. Onactivityresult (Requestcode, ResultCode, data);3 if(ResultCode! =RESULT_OK)4 return;5Bitmap Tempphoto =NULL;6 Switch(requestcode) {7 CasePhoto_picked_with_data:// ????????8Contentresolver CR = This. Getcontentresolver ();9 Try {TenUri uri =Data.getdata (); One Cropimageuri (URI, photo_crop_data); A //Tempphoto = - //Bitmapfactory.decodestream (Cr.openinputstream (URI)); - //Mstepphoto.setuserphoto (Tempphoto); the}Catch(Exception e) { - e.printstacktrace (); - } - Break; + CaseIntent_request_code_camera: -Tempphoto =Bitmapfactory.decodefile (headpath); +Cropimageuri (Uri.fromfile (NewFile (Headpath)), photo_crop_data); A Break; at CasePhoto_crop_data: -Tempphoto = Data.getparcelableextra ("Data"); - Mhead.setimagebitmap (Tempphoto); - Break; - } -}
Article Simple, concrete implementation please refer to the demo, welcome message discussion
Pick up-android from your phone select a picture or take a photo to set your avatar