Android Avatar settings: Get and crop images from the local photo library or photos,

Source: Internet
Author: User

(Switch) android Profile Settings: Get and crop images from the local photo library or take photos,

This article is reproduced in: http://blog.csdn.net/sheeprunning/article/details/9184021

Features

When creating an android Application, user registration is essential. You often need to edit the user's profile picture. The setting process is as follows:

Interface Design

Create a thumbnail ImageView. When you click it, the set Avatar dialog box is displayed. After the settings are complete, refresh the thumbnail;

  

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: background = "# F3F1DA" android: orientation = "vertical"> <! -- Title --> <LinearLayout android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_gravity = "center" android: background = "@ drawable/title_bg" android: gravity = "center" android: orientation = "horizontal"> <TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: gravity = "center" android: text = "@ string/title_bar_txt" android: textColor = "@ androi D: color/white "/> </LinearLayout> <! -- Image switch --> <RelativeLayout android: id = "@ + id/switch_face_rl" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_gravity = "center" android: layout_margin = "20dip" android: background = "@ drawable/item_edit_bg" android: clickable = "true" android: padding = "5dip"> <ImageView android: id = "@ + id/face" android: layout_width = "42dip" android: layout_height = "42dip" android: layout_alignParentLeft = "true" android: layout_marginLeft = "10dip" android: src = "@ drawable/mini_avatar"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginLeft = "5dip" android: layout_marginTop = "5dip" android: layout_toRightOf = "@ id/face" android: layout_gravity = "center" android: text = "click to set the Avatar" android: textColor = "@ android: color/black"/> </RelativeLayout> </LinearLayout>

Activity Design

MainActivity. java:

Package com. xzw. picture; import java. io. file; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import com. xzw. utils. tools; import android. app. activity; import android. app. alertDialog; import android. content. dialogInterface; import android. content. intent; import android. graphics. bitmap; import android. graphics. drawable. bitmapDrawable; import android. graphics. dra Wable. drawable; import android.net. uri; import android. OS. bundle; import android. OS. environment; import android. provider. mediaStore; import android. util. log; import android. view. view; import android. view. window; import android. widget. imageView; import android. widget. relativeLayout; import android. widget. toast;/*** @ author XuZhiwei (xuzw13@gmail.com) Create at 10:14:40 */public class MainActiv Ity extends Activity {/* component */private RelativeLayout switchAvatar; private ImageView faceImage; private String [] items = new String [] {"select local image", "photo "}; /* avatar name */private static final String IMAGE_FILE_NAME = "faceImage.jpg";/* Request Code */private static final int IMAGE_REQUEST_CODE = 0; private static final int CAMERA_REQUEST_CODE = 1; private static final int RESULT_REQUEST_CODE = 2; @ Override p Ublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); // remove the title setContentView (R. layout. main); switchAvatar = (RelativeLayout) findViewById (R. id. switch_face_rl); faceImage = (ImageView) findViewById (R. id. face); // set event listening to switchAvatar. setOnClickListener (listener);} private View. onClickListener listener = new View. onClickLi Stener () {@ Override public void onClick (View v) {showDialog () ;}};/*** display selection dialog box */private void showDialog () {new AlertDialog. builder (this ). setTitle ("set Avatar "). setItems (items, new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {switch (which) {case 0: Intent intentFromGallery = new Intent (); intentFromGallery. setType ("image/*"); // sets the file type IntentFromGallery. setAction (Intent. ACTION_GET_CONTENT); startActivityForResult (intentFromGallery, IMAGE_REQUEST_CODE); break; case 1: intentFromCapture = new Intent (MediaStore. ACTION_IMAGE_CAPTURE); // determines whether the memory card can be used for storage if (Tools. hasSdcard () {File path = Environment. getExternalStoragePublicDirectory (Environment. DIRECTORY_DCIM); File file = new File (path, IMAGE_FILE_NAME); intentFromCapt Ure. putExtra (MediaStore. EXTRA_OUTPUT, Uri. fromFile (file);} startActivityForResult (intentFromCapture, CAMERA_REQUEST_CODE); break ;}}}). setNegativeButton ("cancel", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {dialog. dismiss ();}}). show () ;}@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {// The result code is not Equal to if (resultCode! = RESULT_CANCELED) {switch (requestCode) {case IMAGE_REQUEST_CODE: startPhotoZoom (data. getData (); break; case CAMERA_REQUEST_CODE: if (Tools. hasSdcard () {File path = Environment. getExternalStoragePublicDirectory (Environment. DIRECTORY_DCIM); File tempFile = new File (path, IMAGE_FILE_NAME); startPhotoZoom (Uri. fromFile (tempFile);} else {Toast. makeText (MainActivity. this, "no memory card found, unable to store photos! ", Toast. LENGTH_LONG). show ();} break; case RESULT_REQUEST_CODE: // if (data! = Null) {getImageToView (data) ;}break ;}} super. onActivityResult (requestCode, resultCode, data);}/*** image cropping method implementation ** @ param uri */public void startPhotoZoom (Uri uri) {Intent intent = new Intent ("com. android. camera. action. CROP "); intent. setDataAndType (uri, "image/*"); // you can specify intent for cropping. putExtra ("crop", "true"); // aspectX aspectY is a width-high ratio intent. putExtra ("aspectX", 1); intent. putExtra ("aspectY", 1); // OutputX outputY indicates the width and height of the cropped image intent. putExtra ("outputX", 340); intent. putExtra ("outputY", 340); intent. putExtra ("return-data", true); startActivityForResult (intent, RESULT_REQUEST_CODE );} /*** Save the cropped image data ** @ param picdata */private void getImageToView (Intent data) {Bundle extras = data. getExtras (); if (extras! = Null) {Bitmap photo = extras. getParcelable ("data"); Drawable drawable = new BitmapDrawable (this. getResources (), photo); faceImage. setImageDrawable (drawable );}}}

 

Tools. java

Package com. xzw. utils; import android. OS. environment;/*** @ author XuZhiwei (xuzw13@gmail.com) * Create at 10:14:40 */public class Tools {/*** check whether SDCard * @ return */public static boolean hasSdcard () {String state = Environment exists. getExternalStorageState (); if (state. equals (Environment. MEDIA_MOUNTED) {return true;} else {return false ;}}}

 

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.