Set an avatar for Android, take a photo on your mobile phone, or select an image from a local album as the Avatar,
Zookeeper
[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; // width (X) and height (Y) of the cropped image, 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 (Environme Nt. 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 (inte Nt. getData (); 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 intent for cropping. putExtra ("crop", "true"); // aspectX, aspectY: intent with a high aspect ratio. putExtra ("aspectX", 1); intent. putExtra ("aspectY", 1); // outputX, outputY: Cut The width and height of the image are intent. 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 ;}}}
The layout file has three components: an ImageView with an avatar and two buttons. one of the buttons triggers the operation of selecting an image as the Avatar from the local album; another Button triggers the photo taking on the mobile phone as an avatar. Activity_main.xml:
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <ImageView android: id = "@ + id/imageView" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: src = "@ drawable/ic_launcher"/> <Button android: id = "@ + id/buttonLocal" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "select profile picture from local album"/> <Button android: id = "@ + id/buttonCamera" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "cell phone photo selecting Avatar"/> </LinearLayout>