Set an avatar for Android, take a photo on your phone, or select an image from a local album as the Avatar.

Source: Internet
Author: User

Set an avatar for Android, take a photo on your phone, or select an image from a local album as the Avatar.
??

[Set an avatar for Android, take a photo on your mobile phone, or select an image as an Avatar from a local album]

Social apps such as QQ and Weibo usually have the Avatar setting function. There are two ways to set the Avatar:

1. Let the user select an existing image in the Image Library such as a local album and crop it as the Avatar.

2. Ask the user to start the camera of the mobile phone to take a photo. After taking the photo, cut it and use it as the Avatar.

Now I am writing a simple and complete code example to illustrate how to implement the above two Avatar settings in Android.

MainActivity. java file:

 

Package zhangpgil. photo; import java. io. file; import android. support. v7.app. actionBarActivity; import android. view. view; import android. widget. button; import android. widget. imageView; import android. widget. toast; import android. content. intent; import android. graphics. bitmap; import android.net. uri; import android. OS. bundle; import android. OS. environment; import android. provider. mediaStore; public class MainActi Extends ActionBarActivity {/* profile picture file */private static final String IMAGE_FILE_NAME = temp_head_image.jpg;/* request identifier */private static final int CODE_GALLERY_REQUEST = 0xa0; private static final int CODE_CAMERA_REQUEST = 0xa1; private static final int CODE_RESULT_REQUEST = 0xa2; // The cropped image's width (X) and height (Y), 480X480 square. Private static int output_X = 480; private static int output_Y = 480; private ImageView headImage = null; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); headImage = (ImageView) findViewById (R. id. imageView); Button buttonLocal = (Button) findViewById (R. id. buttonLocal); buttonLocal. setOnClickListener (new View. onClickLi Stener () {@ Overridepublic void onClick (View v) {choseHeadImageFromGallery () ;}}); Button buttonCamera = (Button) findViewById (R. id. buttonCamera); buttonCamera. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {choseHeadImageFromCameraCapture () ;}} // select an image from the local album as the profile private void choseHeadImageFromGallery () {Intent intentFromGallery = new Intent (); // sets the file type intentFromGal Lery. setType (image/*); intentFromGallery. setAction (Intent. ACTION_GET_CONTENT); startActivityForResult (intentFromGallery, CODE_GALLERY_REQUEST);} // start the mobile phone camera to take a photo as the Avatar private void photo () {intentFromCapture = new Intent (MediaStore. ACTION_IMAGE_CAPTURE); // determines whether the memory card is available. if (hasSdcard () {intentFromCapture stores the photo file. putExtra (MediaStore. EXTRA_OUTPUT, Uri. fromFile (new File (Environment. GetExternalStorageDirectory (), IMAGE_FILE_NAME);} startActivityForResult (intentFromCapture, CODE_CAMERA_REQUEST);} @ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent intent) {// if (resultCode = RESULT_CANCELED) {Toast. makeText (getApplication (), cancel, Toast. LENGTH_LONG ). show (); return;} switch (requestCode) {case CODE_GALLERY_REQUEST: cropRawPhoto (intent. g EtData (); break; case CODE_CAMERA_REQUEST: if (hasSdcard () {File tempFile = new File (Environment. getExternalStorageDirectory (), IMAGE_FILE_NAME); cropRawPhoto (Uri. fromFile (tempFile);} else {Toast. makeText (getApplication (), no SDCard !, Toast. LENGTH_LONG). show ();} break; case CODE_RESULT_REQUEST: if (intent! = Null) {setImageToHeadView (intent);} break;} super. onActivityResult (requestCode, resultCode, intent);}/*** crop the original image */public void cropRawPhoto (Uri uri) {Intent intent Intent = new intent (com. android. camera. action. CROP); intent. setDataAndType (uri, image/*); // you can specify the intent for cropping. putExtra (crop, true); // aspectX, aspectY: intent with a high aspect ratio. putExtra (aspectX, 1); intent. putExtra (aspectY, 1); // outputX, outputY: Specifies the width and height of the cropped image. PutExtra (outputX, output_X); intent. putExtra (outputY, output_Y); intent. putExtra (return-data, true); startActivityForResult (intent, CODE_RESULT_REQUEST);}/*** extract and save the cropped image data, and set the View */private void setImageToHeadView (Intent intent) {Bundle extras = intent. getExtras (); if (extras! = Null) {Bitmap photo = extras. getParcelable (data); headImage. setImageBitmap (photo);}/*** tool used to check whether the device has an SDCard */public static boolean hasSdcard () {String state = Environment. getExternalStorageState (); if (state. equals (Environment. MEDIA_MOUNTED) {// SDCardreturn with storage true;} else {return false ;}}}

 

 

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.