Android开发--通过相册或拍照选择头像

        像一些需要显示个人资料的App中,选择并修改自己的个人头像是必须的,最近的项目中也有用到,做个笔记吧。总体的思路是,通过startActivityForResult方法,分别传递调用系统相册的Intent和调用相机拍照的Intent来做选择,之后调用Android系统中自带的图片剪裁,实现图片的剪裁并在onActivityResult方法中获取数据。下面来进行实践:

        1.点击拍照的按钮,则调用系统照相机,并把自己所拍摄的照片保存则自定义路径之下

        private void getPicFromCamera() {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		// 下面这句指定调用<span id="18_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="18_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=26ce0e82a9ad318&k=%CF%E0%BB%FA&k0=%CF%E0%BB%FA&kdi0=0&luki=5&mcpm=0&n=10&p=baidu&q=85048100_cpr&rb=0&rs=1&seller_id=1&sid=18d39a2ae8e06c02&ssp2=1&stid=9&t=tpclicked3_hc&td=1797308&tu=u1797308&u=http%3A%2F%2Fwww%2Ecodes51%2Ecom%2Farticle%2Fdetail%5F99155%2Ehtml&urlid=0" target="_blank" mpid="18" style="color: rgb(0, 0, 0); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">相机</span></a></span>拍照后的照片存储的路径
		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
				Environment.getExternalStorageDirectory(), "test.jpg")));
		startActivityForResult(intent, CAMERA_REQUEST);
	}
         接下来在onActivityResult方法中处理,如果resultCode为-1则表示拍照成功,执行图片剪裁的方法(图片剪裁的方法的稍后叙述)。
		case CAMERA_REQUEST:
			<span id="16_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="16_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=26ce0e82a9ad318&k=switch&k0=switch&kdi0=0&luki=9&mcpm=0&n=10&p=baidu&q=85048100_cpr&rb=0&rs=1&seller_id=1&sid=18d39a2ae8e06c02&ssp2=1&stid=9&t=tpclicked3_hc&td=1797308&tu=u1797308&u=http%3A%2F%2Fwww%2Ecodes51%2Ecom%2Farticle%2Fdetail%5F99155%2Ehtml&urlid=0" target="_blank" mpid="16" style="color: rgb(0, 0, 0); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">switch</span></a></span> (resultCode) {
			case -1://-1表示拍照成功
				File file = new File(Environment.getExternalStorageDirectory()
						+ "/test.jpg");
				if (file.exists()) {
					<span id="17_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="17_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=26ce0e82a9ad318&k=photo&k0=photo&kdi0=0&luki=7&mcpm=0&n=10&p=baidu&q=85048100_cpr&rb=0&rs=1&seller_id=1&sid=18d39a2ae8e06c02&ssp2=1&stid=9&t=tpclicked3_hc&td=1797308&tu=u1797308&u=http%3A%2F%2Fwww%2Ecodes51%2Ecom%2Farticle%2Fdetail%5F99155%2Ehtml&urlid=0" target="_blank" mpid="17" style="color: rgb(0, 0, 0); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">photo</span></a></span>Clip(Uri.fromFile(file));
				}
				break;
			default:
				break;
			}
			break;
            2.点击相册,调用系统相册
	private void getPicFromPhoto() {
		Intent intent = new Intent(Intent.ACTION_PICK, null);
		intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
				"image/*");
		startActivityForResult(intent, PHOTO_REQUEST);
	}
             接下来在onActivityResult方法中处理,如果 data不为null,则表示选取成功,执行图片剪裁的方法(图片剪裁的方法的稍后叙述)。
                case PHOTO_REQUEST:
			if (data != null) {
				<span id="15_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="15_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=26ce0e82a9ad318&k=photo&k0=photo&kdi0=0&luki=7&mcpm=0&n=10&p=baidu&q=85048100_cpr&rb=0&rs=1&seller_id=1&sid=18d39a2ae8e06c02&ssp2=1&stid=9&t=tpclicked3_hc&td=1797308&tu=u1797308&u=http%3A%2F%2Fwww%2Ecodes51%2Ecom%2Farticle%2Fdetail%5F99155%2Ehtml&urlid=0" target="_blank" mpid="15" style="color: rgb(0, 0, 0); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">photo</span></a></span>Clip(data.getData());
			}
			break;
            3.调用系统的图片剪裁进行图片的修理
        private void photoClip(Uri uri) {
		// 调用系统中自带的图片剪裁
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		// 设置在开启的Intent中设置显示的VIEW可裁剪
		intent.putExtra("crop", "true");
		// aspectX aspectY 是宽高的比例
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// outputX outputY 是裁剪图片宽高
		intent.putExtra("outputX", 150);
		intent.putExtra("outputY", 150);
		intent.putExtra("return-<span id="14_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="14_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=26ce0e82a9ad318&k=data&k0=data&kdi0=0&luki=10&mcpm=0&n=10&p=baidu&q=85048100_cpr&rb=0&rs=1&seller_id=1&sid=18d39a2ae8e06c02&ssp2=1&stid=9&t=tpclicked3_hc&td=1797308&tu=u1797308&u=http%3A%2F%2Fwww%2Ecodes51%2Ecom%2Farticle%2Fdetail%5F99155%2Ehtml&urlid=0" target="_blank" mpid="14" style="color: rgb(0, 0, 0); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">data</span></a></span>", true);
		startActivityForResult(intent, PHOTO_CLIP);
	}
              接下来在onActivityResult方法中处理,得到处理后的数据,并显示于ImageView之上
		case PHOTO_CLIP:
			if (data != null) {
				Bundle extras = data.getExtras();
				if (extras != null) {
					Log.w("test", "data");
					Bitmap <span id="13_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="13_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=26ce0e82a9ad318&k=photo&k0=photo&kdi0=0&luki=7&mcpm=0&n=10&p=baidu&q=85048100_cpr&rb=0&rs=1&seller_id=1&sid=18d39a2ae8e06c02&ssp2=1&stid=9&t=tpclicked3_hc&td=1797308&tu=u1797308&u=http%3A%2F%2Fwww%2Ecodes51%2Ecom%2Farticle%2Fdetail%5F99155%2Ehtml&urlid=0" target="_blank" mpid="13" style="color: rgb(0, 0, 0); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">photo</span></a></span> = extras.getParcelable("data");
					mImageView.setImageBitmap(photo);
				}
			}
			break;
附上整体

package com.example.test;

import java.io.File;

import android.<span id="1_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="1_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=26ce0e82a9ad318&k=app&k0=app&kdi0=0&luki=6&mcpm=0&n=10&p=baidu&q=85048100_cpr&rb=0&rs=1&seller_id=1&sid=18d39a2ae8e06c02&ssp2=1&stid=9&t=tpclicked3_hc&td=1797308&tu=u1797308&u=http%3A%2F%2Fwww%2Ecodes51%2Ecom%2Farticle%2Fdetail%5F99155%2Ehtml&urlid=0" target="_blank" mpid="1" style="color: rgb(0, 0, 0); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">app</span></a></span>.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
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.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {

	private Button <span id="2_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="2_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=26ce0e82a9ad318&k=photo&k0=photo&kdi0=0&luki=7&mcpm=0&n=10&p=baidu&q=85048100_cpr&rb=0&rs=1&seller_id=1&sid=18d39a2ae8e06c02&ssp2=1&stid=9&t=tpclicked3_hc&td=1797308&tu=u1797308&u=http%3A%2F%2Fwww%2Ecodes51%2Ecom%2Farticle%2Fdetail%5F99155%2Ehtml&urlid=0" target="_blank" mpid="2" style="color: rgb(0, 0, 0); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">photo</span></a></span>Button;
	private Button cameraButton;
	private ImageView mImageView;

	private static final int PHOTO_REQUEST = 1;
	private static final int CAMERA_REQUEST = 2;
	private static final int PHOTO_CLIP = 3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		photoButton = (Button) findViewById(R.id.main_btn_<span id="3_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="3_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=26ce0e82a9ad318&k=photo&k0=photo&kdi0=0&luki=7&mcpm=0&n=10&p=baidu&q=85048100_cpr&rb=0&rs=1&seller_id=1&sid=18d39a2ae8e06c02&ssp2=1&stid=9&t=tpclicked3_hc&td=1797308&tu=u1797308&u=http%3A%2F%2Fwww%2Ecodes51%2Ecom%2Farticle%2Fdetail%5F99155%2Ehtml&urlid=0" target="_blank" mpid="3" style="color: rgb(0, 0, 0); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">photo</span></a></span>);
		cameraButton = (Button) findViewById(R.id.main_btn_camera);
		mImageView = (ImageView) findViewById(R.id.main_img);
		photoButton.setOnClickListener(this);
		cameraButton.setOnClickListener(this);

	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		<span id="4_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="4_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=26ce0e82a9ad318&k=switch&k0=switch&kdi0=0&luki=9&mcpm=0&n=10&p=baidu&q=85048100_cpr&rb=0&rs=1&seller_id=1&sid=18d39a2ae8e06c02&ssp2=1&stid=9&t=tpclicked3_hc&td=1797308&tu=u1797308&u=http%3A%2F%2Fwww%2Ecodes51%2Ecom%2Farticle%2Fdetail%5F99155%2Ehtml&urlid=0" target="_blank" mpid="4" style="color: rgb(0, 0, 0); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">switch</span></a></span> (arg0.getId()) {
		case R.id.main_btn_camera:
			getPicFromCamera();
			break;

		case R.id.main_btn_<span id="5_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="5_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=26ce0e82a9ad318&k=photo&k0=photo&kdi0=0&luki=7&mcpm=0&n=10&p=baidu&q=85048100_cpr&rb=0&rs=1&seller_id=1&sid=18d39a2ae8e06c02&ssp2=1&stid=9&t=tpclicked3_hc&td=1797308&tu=u1797308&u=http%3A%2F%2Fwww%2Ecodes51%2Ecom%2Farticle%2Fdetail%5F99155%2Ehtml&urlid=0" target="_blank" mpid="5" style="color: rgb(0, 0, 0); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">photo</span></a></span>:
			getPicFromPhoto();
			break;
		default:
			break;
		}
	}

	private void getPicFromPhoto() {
		Intent intent = new Intent(Intent.ACTION_PICK, null);
		intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
				"image/*");
		startActivityForResult(intent, PHOTO_REQUEST);
	}

	private void getPicFromCamera() {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		// 下面这句指定调用<span id="6_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="6_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=26ce0e82a9ad318&k=%CF%E0%BB%FA&k0=%CF%E0%BB%FA&kdi0=0&luki=5&mcpm=0&n=10&p=baidu&q=85048100_cpr&rb=0&rs=1&seller_id=1&sid=18d39a2ae8e06c02&ssp2=1&stid=9&t=tpclicked3_hc&td=1797308&tu=u1797308&u=http%3A%2F%2Fwww%2Ecodes51%2Ecom%2Farticle%2Fdetail%5F99155%2Ehtml&urlid=0" target="_blank" mpid="6" style="color: rgb(0, 0, 0); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">相机</span></a></span>拍照后的照片存储的路径
		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
				Environment.getExternalStorageDirectory(), "test.jpg")));
		startActivityForResult(intent, CAMERA_REQUEST);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent <span id="7_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="7_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=26ce0e82a9ad318&k=data&k0=data&kdi0=0&luki=10&mcpm=0&n=10&p=baidu&q=85048100_cpr&rb=0&rs=1&seller_id=1&sid=18d39a2ae8e06c02&ssp2=1&stid=9&t=tpclicked3_hc&td=1797308&tu=u1797308&u=http%3A%2F%2Fwww%2Ecodes51%2Ecom%2Farticle%2Fdetail%5F99155%2Ehtml&urlid=0" target="_blank" mpid="7" style="color: rgb(0, 0, 0); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">data</span></a></span>) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		<span id="8_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="8_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=26ce0e82a9ad318&k=switch&k0=switch&kdi0=0&luki=9&mcpm=0&n=10&p=baidu&q=85048100_cpr&rb=0&rs=1&seller_id=1&sid=18d39a2ae8e06c02&ssp2=1&stid=9&t=tpclicked3_hc&td=1797308&tu=u1797308&u=http%3A%2F%2Fwww%2Ecodes51%2Ecom%2Farticle%2Fdetail%5F99155%2Ehtml&urlid=0" target="_blank" mpid="8" style="color: rgb(0, 0, 0); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">switch</span></a></span> (requestCode) {
		case CAMERA_REQUEST:
			switch (resultCode) {
			case -1://-1表示拍照成功
				File file = new File(Environment.getExternalStorageDirectory()
						+ "/test.jpg");
				if (file.exists()) {
					<span id="9_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="9_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=26ce0e82a9ad318&k=photo&k0=photo&kdi0=0&luki=7&mcpm=0&n=10&p=baidu&q=85048100_cpr&rb=0&rs=1&seller_id=1&sid=18d39a2ae8e06c02&ssp2=1&stid=9&t=tpclicked3_hc&td=1797308&tu=u1797308&u=http%3A%2F%2Fwww%2Ecodes51%2Ecom%2Farticle%2Fdetail%5F99155%2Ehtml&urlid=0" target="_blank" mpid="9" style="color: rgb(0, 0, 0); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">photo</span></a></span>Clip(Uri.fromFile(file));
				}
				break;
			default:
				break;
			}
			break;
		case PHOTO_REQUEST:
			if (<span id="10_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="10_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=26ce0e82a9ad318&k=data&k0=data&kdi0=0&luki=10&mcpm=0&n=10&p=baidu&q=85048100_cpr&rb=0&rs=1&seller_id=1&sid=18d39a2ae8e06c02&ssp2=1&stid=9&t=tpclicked3_hc&td=1797308&tu=u1797308&u=http%3A%2F%2Fwww%2Ecodes51%2Ecom%2Farticle%2Fdetail%5F99155%2Ehtml&urlid=0" target="_blank" mpid="10" style="color: rgb(0, 0, 0); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">data</span></a></span> != null) {
				photoClip(data.getData());
			}
			break;
		case PHOTO_CLIP:
			if (data != null) {
				Bundle extras = data.getExtras();
				if (extras != null) {
					Log.w("test", "data");
					Bitmap photo = extras.getParcelable("data");
					mImageView.setImageBitmap(photo);
				}
			}
			break;
		default:
			break;
		}

	}

	private void <span id="11_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="11_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=26ce0e82a9ad318&k=photo&k0=photo&kdi0=0&luki=7&mcpm=0&n=10&p=baidu&q=85048100_cpr&rb=0&rs=1&seller_id=1&sid=18d39a2ae8e06c02&ssp2=1&stid=9&t=tpclicked3_hc&td=1797308&tu=u1797308&u=http%3A%2F%2Fwww%2Ecodes51%2Ecom%2Farticle%2Fdetail%5F99155%2Ehtml&urlid=0" target="_blank" mpid="11" style="color: rgb(0, 0, 0); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">photo</span></a></span>Clip(Uri uri) {
		// 调用系统中自带的图片剪裁
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		// 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
		intent.putExtra("crop", "true");
		// aspectX aspectY 是宽高的比例
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// outputX outputY 是裁剪图片宽高
		intent.putExtra("outputX", 150);
		intent.putExtra("outputY", 150);
		intent.putExtra("return-<span id="12_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="12_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=26ce0e82a9ad318&k=data&k0=data&kdi0=0&luki=10&mcpm=0&n=10&p=baidu&q=85048100_cpr&rb=0&rs=1&seller_id=1&sid=18d39a2ae8e06c02&ssp2=1&stid=9&t=tpclicked3_hc&td=1797308&tu=u1797308&u=http%3A%2F%2Fwww%2Ecodes51%2Ecom%2Farticle%2Fdetail%5F99155%2Ehtml&urlid=0" target="_blank" mpid="12" style="color: rgb(0, 0, 0); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">data</span></a></span>", true);
		startActivityForResult(intent, PHOTO_CLIP);
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值