In Android, user portraits and android user portraits are set by accessing a local album or camera.

Source: Internet
Author: User

In Android, user portraits and android user portraits are set by accessing a local album or camera.

Currently, almost all apps have the need to set up portraits when users register. There are three scenarios:

(1) obtain the image of the local album and crop it as the Avatar.

(2) Start the mobile phone camera, take the picture, and then crop it as the Avatar.

(3) add some built-in Avatar Resources in the APP for users to choose from (not user-friendly, rarely used at present ).

This time, we will briefly introduce how to set up an Avatar by getting a local album and taking a camera. The implementation idea is as follows:

(1) The startActivityForResult method is used to pass the Intent that calls the system album and the Intent that calls the camera to make the selection.

(2) Call the image clipping function in the Android system to crop images and obtain data in the onActivityResult method.

For details about how to process the returned results of an Activity, refer to the previous blog article "implementation method of processing returned results of Activity in Android".

The following figure shows the effect of this demo (getting an image from a local album and taking an image from a camera ):

 

The simple layout file is not described here. In this experiment, the camera and local album are called using an implicit intent. You can still call it without adding permissions to the configuration list. The java implementation code is as follows:

1 import android. content. intent; 2 import android. graphics. bitmap; 3 import android.net. uri; 4 import android. OS. bundle; 5 import android. OS. environment; 6 import android. provider. mediaStore; 7 import android. support. v7.app. appCompatActivity; 8 import android. view. view; 9 import android. widget. button; 10 import android. widget. imageView; 11 import android. widget. toast; 12 import java. io. file; 13 14 Public class MainActivity extends AppCompatActivity implements View. onClickListener {15 private Button buttonLocal, buttonCamera; 16 private ImageView imageView; 17 // The Avatar file taken by the camera (this demonstration is stored in the root directory of the SD card) 18 private static final File USER_ICON = new File (Environment. getExternalStorageDirectory (), "user_icon.jpg"); 19 // request identifier (local album, camera, and image cropping respectively) 20 private static final int CODE_PHOTO_REQUEST = 1; 21 private stat Ic final int CODE_CAMERA_REQUEST = 2; 22 private static final int CODE_PHOTO_CLIP = 3; 23 @ Override 24 protected void onCreate (Bundle savedInstanceState) {25 super. onCreate (savedInstanceState); 26 setContentView (R. layout. activity_main); 27 buttonLocal = (Button) findViewById (R. id. buttonLocal); 28 buttonCamera = (Button) findViewById (R. id. buttonCamera); 29 imageView = (ImageView) findViewById (R. id. ImageView); 30 buttonLocal. setOnClickListener (this); 31 buttonCamera. setOnClickListener (this); 32} 33 // set the Click Event 34 @ Override 35 public void onClick (View view) {36 switch (view. getId () {37 case R. id. buttonLocal: 38 // call the method for obtaining the local image 39 getPicFromLocal (); 40 break; 41 case R. id. buttonCamera: 42 // call the camera's photo Method 43 getPicFromCamera (); 44 break; 45 default: 46 break; 47} 48} 49/** 50 * obtain images from the local album 51 */5 2 private void getPicFromLocal () {53 Intent intent = new Intent (); 54 // obtain the local album Method 55 intent. setAction (Intent. ACTION_GET_CONTENT); 56 intent. setType ("image/*"); 57 // method 2 58 // intent. setAction (Intent. ACTION_PICK); 59 // intent. setDataAndType (MediaStore. images. media. EXTERNAL_CONTENT_URI, 60 // "image/*"); 61 startActivityForResult (intent, CODE_PHOTO_REQUEST); 62} 63/** 64 * get an image from a camera, 65 * Save the settings to 66 */67 private void getPicFromCamera () {68 Intent intent = new Intent (); 69 intent. setAction (MediaStore. ACTION_IMAGE_CAPTURE); 70 // The following sentence specifies the path 71 intent for storing the photo after the camera is taken. putExtra (MediaStore. EXTRA_OUTPUT, Uri. fromFile (USER_ICON); 72 startActivityForResult (intent, CODE_CAMERA_REQUEST ); 73} 74/** 75 * image cropping 76*77 * @ param uri 78 */79 private void photoClip (Uri uri) {80 // image cropping in The Calling System 81 Intent intent = new Intent (); 82 intent. setAction ("com. android. camera. action. CROP "); 83 intent. setDataAndType (uri, "image/*"); 84 // The crop = true is set to crop the display VIEW to 85 Intent in the enabled intent. putExtra ("crop", "true"); 86 // aspectX aspectY is a ratio of 87 intent in width. putExtra ("aspectX", 1); 88 intent. putExtra ("aspectY", 1); 89/* outputX outputY is the width and height of the cropped image 90 * this is just a portrait display, it is not recommended to set the value too high 91 * Otherwise the cache size exceeds the 1 MB limit of the binder Mechanism 92 * Report Tra NsactionTooLargeException 93 */94 intent. putExtra ("outputX", 150); 95 intent. putExtra ("outputY", 150); 96 intent. putExtra ("return-data", true); 97 startActivityForResult (intent, CODE_PHOTO_CLIP); 98} 99/** 100 * extract and save the cropped image data, and set View101 */102 private void setImageToHeadView (Intent intent) {103 Bundle extras = intent. getExtras (); 104 if (extras! = Null) {105 Bitmap photo = extras. getParcelable ("data"); 106 imageView. setImageBitmap (photo); 107} 108} 109 @ Override110 protected void onActivityResult (int requestCode, int resultCode, Intent data) {111 // the user does not perform valid settings, returns 112 if (resultCode = RESULT_CANCELED) {113 Toast. makeText (MainActivity. this, "cancel", Toast. LENGTH_LONG ). show (); 114 return; 115} 116 switch (requestCode) {117 case CODE_CAMERA_REQUES T: 118 if (USER_ICON.exists () {119 photoClip (Uri. fromFile (USER_ICON); 120} 121 break; 122 case CODE_PHOTO_REQUEST: 123 if (data! = Null) {124 photoClip (data. getData (); 125} 126 break; 127 case CODE_PHOTO_CLIP: 128 if (data! = Null) {129 setImageToHeadView (data); 130} 131 break; 132} 133 super. onActivityResult (requestCode, resultCode, data); 134} 135}

Note that when cropping an image, do not set the length and width too large. Otherwise, the cache size exceeds the limit of the binder Mechanism (affected by the mobile phone configuration ). transactionTooLargeException is reported, which has been detailed in the Code. Please pay attention to it when implementing it.

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.