Android practice simple tutorial-27th gun (Android sets the Avatar upload function), android practice

Source: Internet
Author: User

Android practice simple tutorial-27th gun (Android sets the Avatar upload function), android practice

In general development, the function of uploading an image as an avatar is very common. Next we will study the specific implementation and the code is very simple. You can use it directly!

1. Take a look at the layout file, which is 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 = "album"/> <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. onClickLi Stener; 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_REQUEST_CODE = 1; private static final int RESIZE_REQUEST_CODE = 2; private static final String IMAGE_FILE_NAME = "header.jpg"; private ImageView mImageHead Er; @ 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.setOnClickLis Tener (this); selectBtn2.setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. btn_selectimage: Intent galleryIntent = new Intent (Intent. ACTION_GET_CONTENT); galleryIntent. addCategory (Intent. CATEGORY_OPENABLE); galleryIntent. setType ("image/*"); // image startActivityForResult (galleryIntent, IMAGE_REQUEST_CODE); break; case R. id. btn_takephoto: if (isSdcardExisting () {Inte Nt cameraIntent = new Intent ("android. media. action. IMAGE_CAPTURE "); // take a photo of cameraIntent. putExtra (MediaStore. EXTRA_OUTPUT, getImageUri (); cameraIntent. putExtra (MediaStore. EXTRA_VIDEO_QUALITY, 0); startActivityForResult (cameraIntent, CAMERA_REQUEST_CODE);} else {Toast. makeText (v. getContext (), "insert SD card", Toast. LENGTH_LONG ). show ();} break; }}@ Overrideprotected void onActivityResult (int requestCode, int result Code, Intent data) {if (resultCode! = RESULT_ OK) {return;} else {switch (requestCode) {case IMAGE_REQUEST_CODE: resizeImage (data. getData (); break; case CAMERA_REQUEST_CODE: if (isSdcardExisting () {resizeImage (getImageUri ();} else {Toast. makeText (MainActivity. this, "no memory card found, unable to store photos! ", Toast. LENGTH_LONG). show ();} break; case RESIZE_REQUEST_CODE: if (data! = Null) {showResizeImage (data) ;}break ;}} super. onActivityResult (requestCode, resultCode, data);} private boolean isSdcardExisting () {// determine whether the SD card has final String state = Environment. getExternalStorageState (); if (state. equals (Environment. MEDIA_MOUNTED) {return true;} else {return false;} public void resizeImage (Uri uri) {// reshapes the image size Intent intent = new Intent ("com. android. camera. action. CROP "); intent. setDataAn DType (uri, "image/*"); intent. putExtra ("crop", "true"); // intent can be cropped. putExtra ("aspectX", 1); intent. putExtra ("aspectY", 1); intent. putExtra ("outputX", 150); intent. putExtra ("outputY", 150); intent. putExtra ("return-data", true); startActivityForResult (intent, RESIZE_REQUEST_CODE);} private void showResizeImage (Intent data) {// display image Bundle extras = data. getExtras (); if (extras! = Null) {Bitmap photo = extras. getParcelable ("data"); Drawable drawable = new BitmapDrawable (photo); mImageHeader. setImageDrawable (drawable);} private Uri getImageUri () {// get the path return Uri. fromFile (new File (Environment. getExternalStorageDirectory (), IMAGE_FILE_NAME ));}}

3. Run the instance:

If you like it, please like me! Thank you!

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.