<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/corner_stroke" android:gravity="center" android:orientation="horizontal" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="我的靓照" android:textColor="@android:color/black" /> </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/corner_stroke_black" android:clickable="true" android:padding="5dip" > <ImageView android:id="@+id/face" android:layout_width="60dip" android:layout_height="60dip" android:layout_alignParentLeft="true" android:layout_marginLeft="10dip" android:background="@drawable/corner_stroke" android:src="@drawable/family_ice_say_relatives" /> <TextView android:layout_width="wrap_content" android:layout_height="60dp" android:layout_marginLeft="5dip" android:layout_marginTop="5dip" android:layout_toRightOf="@id/face" android:layout_gravity="center" android:text="点击设置头像" android:textColor="@android:color/black" /> </RelativeLayout> </LinearLayout>
?
package com.mm.mmcalendar.activity;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import android.app.AlertDialog;import android.content.DialogInterface;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.support.v4.app.FragmentActivity;import android.view.View;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.Toast;import com.mm.mmcalendar.R;import com.mm.mmcalendar.dao.SharedPreferencesUtils;import com.mm.mmcalendar.utils.Tools;public class MyFaceActivity extends FragmentActivity { /* 组件 */ private RelativeLayout switchAvatar; private ImageView faceImage; private String[] items = new String[] { "选择本地图片", "拍照" }; /* 头像名称 */ private static final String IMAGE_FILE_NAME = "faceImage.jpg"; /* 请求码 */ 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 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //requestWindowFeature(Window.FEATURE_NO_TITLE); // 去掉标题 setContentView(R.layout.myface); switchAvatar = (RelativeLayout) findViewById(R.id.switch_face_rl); faceImage = (ImageView) findViewById(R.id.face); String photo=SharedPreferencesUtils.getStringValue(this, SharedPreferencesUtils.PHOTO_PATH, null); if(photo!=null){ byte[] content = Tools.getImageContentInSize(photo, 310, 310); Bitmap b=Tools.Bytes2Bimap(content); if(b!=null) faceImage.setImageBitmap(b); } // 设置事件监听 switchAvatar.setOnClickListener(listener); } private View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { showDialog(); } }; /** * 显示选择对话框 */ private void showDialog() { new AlertDialog.Builder(this) .setTitle("设置头像") .setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: Intent intentFromGallery = new Intent(); intentFromGallery.setType("image/*"); // 设置文件类型 intentFromGallery .setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(intentFromGallery, IMAGE_REQUEST_CODE); break; case 1: Intent intentFromCapture = new Intent( MediaStore.ACTION_IMAGE_CAPTURE); // 判断存储卡是否可以用,可用进行存储 if (Tools.hasSdcard()) { File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); File file = new File(path,IMAGE_FILE_NAME); intentFromCapture.putExtra( MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); } startActivityForResult(intentFromCapture,CAMERA_REQUEST_CODE); break; } } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).show(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { //结果码不等于取消时候 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(MyFaceActivity.this, "未找到存储卡,无法存储照片!",Toast.LENGTH_LONG).show(); } break; case RESULT_REQUEST_CODE: //图片缩放完成后 if (data != null) { getImageToView(data); } break; } } super.onActivityResult(requestCode, resultCode, data); } /** * 裁剪图片方法实现 * * @param uri */ public void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // 设置裁剪 intent.putExtra("crop", "true"); // aspectX aspectY 是宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY 是裁剪图片宽高 intent.putExtra("outputX", 340); intent.putExtra("outputY", 340); intent.putExtra("return-data", true); startActivityForResult(intent, RESULT_REQUEST_CODE); } /** * 保存裁剪之后的图片数据 * * @param picdata */ private void getImageToView(Intent data) { Bundle extras = data.getExtras(); if (extras != null) { Bitmap photo = extras.getParcelable("data"); savePic(photo); photo=Tools.toRoundCorner(photo,7); Drawable drawable = new BitmapDrawable(this.getResources(),photo); faceImage.setImageDrawable(drawable); } } private void savePic(Bitmap photo ){ long l2 = System.currentTimeMillis(); String fileName = l2 + ".jpg"; String tempImgPath = getCacheDir().getAbsolutePath() + "/sysfiles/temp/" + fileName; String dir = getDir(tempImgPath); File dirFile = new File(dir); dirFile.mkdirs(); if (!dirFile.exists()) { Toast.makeText(MyFaceActivity.this, "无法创建SD卡目录,图片无法保存", Toast.LENGTH_LONG).show(); } try { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(tempImgPath)); photo.compress(Bitmap.CompressFormat.JPEG, 75, bos);// (0 - 100)压缩文件 SharedPreferencesUtils.setStringValue(MyFaceActivity.this, SharedPreferencesUtils.PHOTO_PATH, tempImgPath); } catch (Exception e) { e.printStackTrace(); } } public String getDir(String filePath) { int lastSlastPos = filePath.lastIndexOf('/'); return filePath.substring(0, lastSlastPos); }}
?