Android Easy Tutorial-the 27th gun (Android settings avatar upload function implementation)

Source: Internet
Author: User

In the general development of the upload image as a picture of the function is very common, the following we study the implementation of the specific code is very simple, we can directly use Oh!

1. Look at the layout file first, it's ugly:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent " >    <imageview        android:id= "@+id/image_header"        android:layout_width= "150DP"        android:layout_ height= "150DP"/>    <button        android:id= "@+id/btn_selectimage" android:layout_width= "Wrap_        Content "        android:layout_height=" wrap_content "        android:layout_below=" @+id/image_header "        android: text= "Albums"/>    <button        android:id= "@+id/btn_takephoto" android:layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:layout_below= "@+id/btn_selectimage"        android:text= "Photo"/></relativelayout>
2.mainactivity.java:

Package Com.example.userphoto;import Java.io.file;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.drawable.bitmapdrawable;import Android.graphics.drawable.drawable;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.imageview;import Android.widget.toast;public class MainActivity extends Activity implements Onclicklistener {private static final int image_request_code = 0;private static final int camera_reque St_code = 1;private static final int resize_request_code = 2;private static final String image_file_name = "Header.jpg";p R Ivate ImageView mimageheader; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); Setupviews ();} private void Setupviews () {Mimageheader = (ImagEview) Findviewbyid (R.id.image_header); final Button selectBtn1 = (Button) Findviewbyid (r.id.btn_selectimage); final Button selectBtn2 = (button) Findviewbyid (R.id.btn_takephoto); Selectbtn1.setonclicklistener (this); Selectbtn2.setonclicklistener (this);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case R.id.btn_selectimage:intent galleryintent = new Intent (Int Ent. action_get_content); galleryintent.addcategory (intent.category_openable); Galleryintent.settype ("image/*");// Picture Startactivityforresult (Galleryintent, Image_request_code); Break;case r.id.btn_takephoto:if (isSdcardExisting ()) {Intent cameraintent = new Intent ("Android.media.action.IMAGE_CAPTURE");//Photo Cameraintent.putextra (Mediastore.extra _output, Getimageuri ()); Cameraintent.putextra (mediastore.extra_video_quality, 0); Startactivityforresult ( Cameraintent, Camera_request_code);} else {Toast.maketext (V.getcontext (), "Please insert SD card", Toast.length_long). Show (); Break;}} @Overrideprotected void Onactivityresult (int requestcode, int resulTcode, Intent data) {if (ResultCode! = RESULT_OK) {return;} else {switch (requestcode) {case Image_request_code:resizeimag E (Data.getdata ()); Break;case Camera_request_code:if (Issdcardexisting ()) {Resizeimage (Getimageuri ());} else { Toast.maketext (Mainactivity.this, "No memory card found, no photos saved! ", Toast.length_long). Show ();} Break;case resize_request_code:if (data = null) {showresizeimage (data);} Break;}} Super.onactivityresult (Requestcode, ResultCode, data);} Private Boolean issdcardexisting () {//determines if the SD card has a final String state = Environment.getexternalstoragestate (); State.equals (environment.media_mounted)) {return true;} else {return false;}} public void Resizeimage (URI uri) {//Reshape picture size intent Intent = new Intent ("Com.android.camera.action.CROP"); Intent.setdataandtype (URI, "image/*"); Intent.putextra ("Crop", "true");//Can Crop Intent.putextra ("Aspectx", 1); Intent.putextra ("Aspecty", 1), Intent.putextra ("Outputx", "Intent.putextra"; Return-data ", true); Startactivityforresult (IntenT, Resize_request_code);} private void Showresizeimage (Intent data) {//Display picture Bundle extras = Data.getextras (); if (extras! = null) {Bitmap photo = Extr As.getparcelable ("Data");D rawable drawable = new bitmapdrawable (photo); mimageheader.setimagedrawable (drawable);}} Private Uri Getimageuri () {//Get path return Uri.fromfile (New File (Environment.getexternalstoragedirectory (), Image_file_ NAME));}}

3. Run the instance on the line:

Like friends please click on me! Thank you!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Easy Tutorial-the 27th gun (Android settings avatar upload function implementation)

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.