Chao Hua Xi-android selects an image from the mobile phone or takes a photo to set the Avatar,

Source: Internet
Author: User

Chao Hua Xi-android selects an image from the mobile phone or takes a photo to set the Avatar,

Demo source code location: http://git.oschina.net/zj2012zy/Android-Demo/tree/master/AndroidDemo/headset

Generally, you need to set a user profile even if you need a lot of user information. Generally, you can either select an image from your mobile phone or take a photo directly on your mobile phone. As follows:

In addition, you can use the dynamic screenshot function of APP Bao to create a mobile phone screenshot, which is very convenient.

The layout file is simple: Add two buttons and an imageview to display the set avatar.

The core code is as follows:

1. Select an image

1     public static void selectPhoto(Activity activity) {2         Intent intent = new Intent(Intent.ACTION_GET_CONTENT);3         intent.setType("image/jpeg");4         intent.putExtra("return-data", true);5         activity.startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);6     }

2. Take and store photos

 1     public static String takePicture(Activity activity) { 2         createDirFile(Environment.getExternalStorageDirectory().getPath() + "/zhaojie/images/"); 3         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 4         String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); 5         String path = Environment.getExternalStorageDirectory().getPath() + "/zhaojie/images/" + timeStamp + ".png"; 6         File file = createNewFile(path); 7         if (file != null) { 8             intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); 9         }10         activity.startActivityForResult(intent, INTENT_REQUEST_CODE_CAMERA);11         return path;12     }

3. process the photo or selected photo and capture the Avatar. You can specify the size of the Avatar.

 1     private void cropImageUri(Uri uri, int requestCode) { 2         Intent intent = new Intent("com.android.camera.action.CROP"); 3         intent.setDataAndType(uri, "image/*"); 4         intent.putExtra("crop", "true"); 5         intent.putExtra("aspectX", 1); 6         intent.putExtra("aspectY", 1); 7         intent.putExtra("outputX", 320); 8         intent.putExtra("outputY", 320); 9         intent.putExtra("scale", true);10         intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);11         intent.putExtra("return-data", true);12         intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());13         intent.putExtra("noFaceDetection", true); // no face detection14         startActivityForResult(intent, requestCode);15     }

4. The overall process is basically implemented by calling the system activity. The Code is as follows:

 1     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 2         super.onActivityResult(requestCode, resultCode, data); 3         if (resultCode != RESULT_OK) 4             return; 5         Bitmap tempPhoto = null; 6         switch (requestCode) { 7             case PHOTO_PICKED_WITH_DATA: // ???????? 8                 ContentResolver cr = this.getContentResolver(); 9                 try {10                     Uri uri = data.getData();11                     cropImageUri(uri, PHOTO_CROP_DATA);12                     // tempPhoto =13                     // BitmapFactory.decodeStream(cr.openInputStream(uri));14                     // mStepPhoto.setUserPhoto(tempPhoto);15                 } catch (Exception e) {16                     e.printStackTrace();17                 }18                 break;19             case INTENT_REQUEST_CODE_CAMERA:20                 tempPhoto = BitmapFactory.decodeFile(headPath);21                 cropImageUri(Uri.fromFile(new File(headPath)), PHOTO_CROP_DATA);22                 break;23             case PHOTO_CROP_DATA:24                 tempPhoto = data.getParcelableExtra("data");25                 mHead.setImageBitmap(tempPhoto);26                 break;27         }28     }

The article is simplified. For more information, see the demo.

 

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.