Android Avatar selection and cropping

Source: Internet
Author: User

First, the layout statement

< ImageView                 Android:id = "@+id/head_image"                 android:layout_width= "80DP"                android:layout_height= "80DP"                 Android:layout_centerhorizontal = "true"                 android:background= "@drawable/default_avatar"/>

Second, the activity in the code

PrivateImageView Headimageview; PrivateBitmaputil Bitmaputil =NewBitmaputil (this); Headimageview=(ImageView) Findviewbyid (r.id.head_image); Headimageview.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {NewAndroid.app.AlertDialog.Builder (registeractivity. This). Settitle ("Avatar Selection"). Setnegativebutton ("Album selection",                        NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich)                                {Dialog.cancel (); Bitmaputil.docropphoto (registeractivity. This); }}). Setpositivebutton ("Camera Photo",                        NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich)                                {Dialog.cancel (); String Status=environment.getexternalstoragestate (); if(Status.equals (environment.media_mounted)) {//determine if there is an SD cardBitmaputil.dotakephoto (registeractivity. This);//the user clicked from the camera to get}} ). Show (); }        });

Third, activity callback method

/*** Avatar Selection callback * ResultCode: Normal return-1 user back 0*/@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {LOG.I ("TAG", Requestcode + ":" +ResultCode); Switch(requestcode) { CaseBitmaputil.activity_result_camara_with_data://Take Pictures            Try {                if(ResultCode = =-1) {Bitmaputil.cropimageuribytakephoto (registeractivity. This); }            } Catch(Exception e) {e.printstacktrace (); }             Break;  CaseBitmaputil.activity_result_cropimage_with_data:Try {                if(ResultCode = =-1) {Bitmap Bitmap= Bitmaputil.decodeuriasbitmap (registeractivity. This); if(Bitmap! =NULL) {headimageview.setimagebitmap (bitmap); }                }            } Catch(Exception e) {e.printstacktrace (); }             Break; }    }

Four, the tool class

ImportJava.io.File;Importjava.io.FileNotFoundException;Importjava.io.IOException;Importandroid.app.Activity;Importandroid.content.ActivityNotFoundException;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.net.Uri;Importandroid.os.Environment;ImportAndroid.os.SystemClock;ImportAndroid.provider.MediaStore;ImportAndroid.util.Log; Public classBitmaputil {//open to the outside     PublicUri Photouri; PrivateFile Picfile; PrivateUri Tempuri;  Public Final Static intActivity_result_camara_with_data = 1006;  Public Final Static intActivity_result_cropimage_with_data = 1007; Privatecontext Context;  PublicBitmaputil (Context context) {Super();  This. Context =context; }    /*** Take photos to get pictures **/     Public voidDotakephoto () {Try{File Uploadfiledir=NewFile (Environment.getexternalstoragedirectory (), "/upload"); Intent cameraintent=NewIntent (mediastore.action_image_capture); if(!uploadfiledir.exists ())            {Uploadfiledir.mkdirs (); } picfile=NewFile (Uploadfiledir, Systemclock.currentthreadtimemillis () + ". png"); if(!picfile.exists ())            {Picfile.createnewfile (); } Tempuri=Uri.fromfile (Picfile);            Cameraintent.putextra (Mediastore.extra_output, Tempuri);            (Activity) context). Startactivityforresult (Cameraintent, activity_result_camara_with_data); LOG.I ("TAG", "photo1:" + tempuri.tostring () + ", Photo2:" +Tempuri.getpath ()); } Catch(activitynotfoundexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }    }     Public voidDocropphoto () {Try{File Picturefiledir=NewFile (Environment.getexternalstoragedirectory (), "/upload"); if(!picturefiledir.exists ())            {Picturefiledir.mkdirs (); } picfile=NewFile (Picturefiledir, Systemclock.currentthreadtimemillis () + ". png"); if(!picfile.exists ())            {Picfile.createnewfile (); } Tempuri=Uri.fromfile (Picfile); FinalIntent Intent =getcropimageintent ();            (Activity) context). Startactivityforresult (Intent, activity_result_cropimage_with_data); LOG.I ("TAG", "Photo3:" + tempuri.tostring () + ", photo4:" +Tempuri.getpath ()); } Catch(Exception e) {e.printstacktrace (); }    }    /*** Constructs an intent for image cropping. Call Picture Clipping program*/    PrivateIntent getcropimageintent () {Intent Intent=NewIntent (Intent.action_get_content,NULL); Intent.settype ("Image/*"); Intent.putextra ("Crop", "true"); Intent.putextra ("Aspectx", 1); Intent.putextra ("Aspecty", 1); Intent.putextra ("Outputx", 100); Intent.putextra ("Outputy", 100); Intent.putextra ("Nofacedetection",true); Intent.putextra ("Scale",true); Intent.putextra ("Return-data",false);        Intent.putextra (Mediastore.extra_output, Tempuri); Intent.putextra ("OutputFormat", Bitmap.CompressFormat.JPEG.toString ()); returnIntent; }     Public voidCropimageuribytakephoto () {Intent Intent=NewIntent ("Com.android.camera.action.CROP"); Intent.setdataandtype (Tempuri,"Image/*"); Intent.putextra ("Crop", "true"); Intent.putextra ("Aspectx", 1); Intent.putextra ("Aspecty", 1); Intent.putextra ("Outputx", 100); Intent.putextra ("Outputy", 100); Intent.putextra ("Scale",true);        Intent.putextra (Mediastore.extra_output, Tempuri); Intent.putextra ("Return-data",false); Intent.putextra ("OutputFormat", Bitmap.CompressFormat.JPEG.toString ()); Intent.putextra ("Nofacedetection",true);//No Face Detection( Activity) context). Startactivityforresult (Intent, activity_result_cropimage_with_data); }     PublicBitmap Decodeuriasbitmap () {Bitmap Bitmap=NULL; Try {            if(Tempuri! =NULL) {Photouri=Tempuri; Bitmap=Bitmapfactory.decodestream (Context.getcontentresolver (). Openinputstream (Photouri)); }        } Catch(FileNotFoundException e) {e.printstacktrace (); return NULL; }        returnbitmap; }}

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.