[Android Notes] You can take a photo on your Android phone or select an image from the album as your avatar,

Source: Internet
Author: User

[Android Notes] You can take a photo on your Android phone or select an image from the album as your avatar,

1 package zhangpgil. photo; 2 3 import java. io. file; 4 import android. support. v7.app. actionBarActivity; 5 import android. view. view; 6 import android. widget. button; 7 import android. widget. imageView; 8 import android. widget. toast; 9 import android. content. intent; 10 import android. graphics. bitmap; 11 import android.net. uri; 12 import android. OS. bundle; 13 import android. OS. environment; 14 import android Oid. provider. mediaStore; 15 16 public class MainActivity extends ActionBarActivity {17 18/* profile picture file */19 private static final String IMAGE_FILE_NAME = "temp_head_image.jpg "; 20 21/* request identifier */22 private static final int CODE_GALLERY_REQUEST = 0xa0; 23 private static final int CODE_CAMERA_REQUEST = 0xa1; 24 private static final int CODE_RESULT_REQUEST = 0xa2; 25 26 // width (X) and height (Y) of the cropped image, 480X480 square.(Sometimes an error occurs when generating bitmap? Try to reduce the size)27 private static int output_X = 480; 28 private static int output_Y = 480; 29 30 private ImageView headImage = null; 31 32 @ Override 33 protected void onCreate (Bundle savedInstanceState) {34 super. onCreate (savedInstanceState); 35 setContentView (R. layout. activity_main); 36 37 headImage = (ImageView) findViewById (R. id. imageView); 38 39 Button buttonLocal = (Button) findViewById (R. id. buttonLocal); 4 0 buttonLocal. setOnClickListener (new View. onClickListener () {41 42 @ Override 43 public void onClick (View v) {44 choseHeadImageFromGallery (); 45} 46}); 47 48 Button buttonCamera = (Button) findViewById (R. id. buttonCamera); 49 buttonCamera. setOnClickListener (new View. onClickListener () {50 51 @ Override 52 public void onClick (View v) {53 choseHeadImageFromCameraCapture (); 54} 55}); 56} 57 58/ /Select an image from the local album as the Avatar 59 private void choseHeadImageFromGallery () {60 Intent intentFromGallery = new Intent (); 61 // set the file type 62 intentFromGallery. setType ("image/*"); 63 intentFromGallery. setAction (Intent. ACTION_GET_CONTENT); 64 startActivityForResult (intentFromGallery, CODE_GALLERY_REQUEST); 65} 66 67 // start the mobile phone camera to take a photo as the Avatar 68 private void photo () {69 Intent intentFromCapture = ne W Intent (MediaStore. ACTION_IMAGE_CAPTURE); 70 71 // judge whether the memory card is available and store the photo file 72 if (hasSdcard () {73 intentFromCapture. putExtra (MediaStore. EXTRA_OUTPUT, Uri 74. fromFile (new File (Environment 75. getExternalStorageDirectory (), IMAGE_FILE_NAME); 76} 77 78 startActivityForResult (intentFromCapture, CODE_CAMERA_REQUEST); 79} 80 81 @ Override 82 protected void onActivityResult (int requestCode, int resultCod E, 83 Intent intent) {84 85 // the user does not perform a valid setting operation, and 86 if (resultCode = RESULT_CANCELED) {87 Toast is returned. makeText (getApplication (), "cancel", Toast. LENGTH_LONG ). show (); 88 return; 89} 90 91 switch (requestCode) {92 case CODE_GALLERY_REQUEST: 93 cropRawPhoto (intent. getData (); 94 break; 95 96 case CODE_CAMERA_REQUEST: 97 if (hasSdcard () {98 File tempFile = new File (99 Environment. getExternalStorageDirect Ory (), 100 IMAGE_FILE_NAME); 101 cropRawPhoto (Uri. fromFile (tempFile); 102} else {103 Toast. makeText (getApplication (), "No SDCard! ", Toast. LENGTH_LONG) 104. show (); 105} 106 107 break; 108 109 case CODE_RESULT_REQUEST: 110 if (intent! = Null) {111 setImageToHeadView (intent); 112} 113 114 break; 115} 116 117 super. onActivityResult (requestCode, resultCode, intent); 118} 119 120/** 121 * crop the original image 122 */123 public void cropRawPhoto (Uri uri) {124 125 Intent intent = new Intent ("com. android. camera. action. CROP "); 126 intent. setDataAndType (uri, "image/*"); 127 128 // you can specify 129 intent for cropping. putExtra ("crop", "true"); 130 131 // aspectX, aspectY: Ratio of width to height 13 2 intent. putExtra ("aspectX", 1); 133 intent. putExtra ("aspectY", 1); 134 135 // outputX, outputY: Crop an image with a width of 136 intent. putExtra ("outputX", output_X); 137 intent. putExtra ("outputY", output_Y); 138 intent. putExtra ("return-data", true); 139 140 startActivityForResult (intent, CODE_RESULT_REQUEST); 141} 142 143/** 144 * extract and save the cropped image data, and set View145 */146 private void setImageToHeadView (Intent intent) {147 Bund Le extras = intent. getExtras (); 148 if (extras! = Null) {149 Bitmap photo = extras. getParcelable ("data"); 150 headImage. setImageBitmap (photo); 151} 152} 153 154/** 155 * tool for checking whether the device has SDCard 156 */157 public static boolean hasSdcard () {158 String state = Environment. getExternalStorageState (); 159 if (state. equals (Environment. MEDIA_MOUNTED) {160 // SDCard161 return true; 162} else {163 return false; 164} 165}
1 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 android: layout_width = "match_parent" 3 android: layout_height = "match_parent" 4 android: orientation = "vertical"> 5 6 <ImageView 7 android: id = "@ + id/imageView" 8 android: layout_width = "wrap_content" 9 android: layout_height = "wrap_content" 10 android: src = "@ drawable/ic_launcher"/> 11 12 <Button13 android: id = "@ + id/buttonLocal" 14 android: layout_width = "wrap_content" 15 android: layout_height = "wrap_content" 16 android: text = "Local album selected Avatar"/> 17 18 <Button19 android: id = "@ + id/buttonCamera" 20 android: layout_width = "wrap_content" 21 android: layout_height = "wrap_content" 22 android: text = "cell phone photo select Avatar"/> 23 24 </LinearLayout>

Http://www.open-open.com/lib/view/open1428569177447.html ()

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.