Android upload avatar Code, camera, album, crop

Source: Internet
Author: User

Activity_main.xml

<?xml version="1.0"Encoding="Utf-8"? ><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"Android:layout_width="Fill_parent"android:layout_height="Fill_parent"android:orientation="Vertical"> <Button android:id="@+id/btn_01"Android:layout_width="150dip"android:layout_height="50dip"android:text="Album"/> <Button android:id="@+id/btn_02"Android:layout_width="150dip " android:layout_height=  "50dip"  Android:text= photo imageview android:id= " @+id/imageid Android : Layout_width= "match_parent  " android:layout_height=  "match_parent"/> </linearlayout>             

Mainactivity.java

Package Com.example.uploadhead;import Java.io.bytearrayinputstream;import Java.io.bytearrayoutputstream;import Java.io.file;import Java.io.inputstream;import Java.text.simpledateformat;import Java.util.Date;import Android.net.uri;import Android.os.bundle;import Android.os.environment;import Android.provider.MediaStore;import Android.app.activity;import Android.content.intent;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.view.view;import Android.widget.button;import Android.widget.ImageView;PublicClassMainactivity extends Activity {PublicStatic finalint NONE =0;PublicStatic finalint Photo_camera =1;//Camera photoPublicStatic finalint photo_compile =2;//Edit PicturePublicStatic finalint Photo_resoult =3;//ResultsImageView Headimageview =Null; Button Photo_button =Null; Button Camera_button =Null;PrivateString ImageName;PublicStaticString Getstringtoday () {Date currenttime =NewDate (); SimpleDateFormat formatter =New SimpleDateFormat ("Yyyymmddhhmmss"); String datestring =Formatter.format (currenttime);Returndatestring; } @OverridePublicvoidOnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Headimageview = (ImageView) Findviewbyid (R.id.imageid);//Find the ImageView to fillPhoto_button = (Button) Findviewbyid (r.id.btn_01);//Album Button Camera_button = (button) Findviewbyid (r.id.btn_02);//Photo button//Open the System album Photo_button.setonclicklistener (NewView.onclicklistener () {PublicvoidOnClick (View v) {//Set the intention to invoke the System album (implicit intent) Intent Intent =NewIntent ();//Set Value activity//Android.intent.action.PICKIntent.setaction (Intent.action_pick);//Set type and dataIntent.setdataandtype (MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");//Open the system's albumStartactivityforresult (Intent, photo_compile); } });//Turn on the system camera Camera_button.setonclicklistener (NewView.onclicklistener () {PublicvoidOnClick (View v) {//Set the name of the picture ImageName ="/"+ getstringtoday () +". jpg";//Set the intent to invoke the system camera (implicit intent) Intent Intent =NewIntent (mediastore.action_image_capture);//Set the output path and file name of the photoFile file=NewFile (Environment.getexternalstoragedirectory (), ImageName); Intent.putextra (Mediastore.extra_output, Uri.fromfile (file));//Turn on the cameraStartactivityforresult (Intent, Photo_camera); } }); }//Call Startactivityresult, return the callback function after@OverrideProtectedvoid Onactivityresult (int Requestcode,IntResultCode, Intent data) {if (ResultCode = =NONE)Return;//Picture taking pictures by cameraif (Requestcode = =Photo_camera) {//Set file save path here in the directory file picture =NewFile (environment.getexternalstoragedirectory () +ImageName);//Crop a pictureStartphotozoom (Uri.fromfile (picture)); }if (data = =Null)Return;//Reading album cropping picturesif (Requestcode = =Photo_compile) {//Crop a pictureStartphotozoom (Data.getdata ()); }//Crop photo processing resultsif (Requestcode = =Photo_resoult) {Bundle Extras =Data.getextras ();if (Extras! =Null) {Bitmap photo = Extras.getparcelable ("Data"); Bytearrayoutputstream stream =NewBytearrayoutputstream (); Photo.compress (Bitmap.CompressFormat.JPEG,, stream);//(0-//100) Compress files//Set up picture display contentHeadimageview.setimagebitmap (photo); }} super.onactivityresult (Requestcode, ResultCode, data); }/** Call the system's crop picture function * * @param the path of the URI picture*/PublicvoidStartphotozoom (Uri uri) {Intent Intent =New Intent ("Com.android.camera.action.CROP"); Intent.setdataandtype (URI,  "image/* "); Intent.putextra ( "crop " true "); // Aspectx Aspecty is a proportional Intent.putextra ( "aspectx", 1); Intent.putextra ("Aspecty", 1); Outputx Outputy is a cropped picture with a wide height of Intent.putextra ("Outputx", 64); Intent.putextra ("Outputy", 64); Intent.putextra ("Return-data", true); Startactivityforresult (Intent, Photo_resoult); }} 

Android upload avatar Code, camera, album, crop

Related Article

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.