首页 > Android > android 通过本地图片或者拍照方式设置头像

android 通过本地图片或者拍照方式设置头像

通过本地图片或者拍照上传方式设置头像,是app非常普遍的一个功能,不多说废话,直接贴代码

public class MainActivity extends Activity {

	private static final int IMAGE_REQUEST_CODE = 0;
	private static final int CAMERA_REQUEST_CODE = 1;
	private static final int RESULT_REQUEST_CODE = 2;
	private String[] items = new String[] { "选择本地图片", "拍照" };
	private static final String IMAGE_FILE_NAME = "pic.jpg"; // 拍照默认图片名称
	private static final String IMAGE_AVATAR = "avatar.jpg";// 裁剪之后头像默认名称
	private ImageView imageAvatar;
	private String imagePath;//头像文件本地路径

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		imageAvatar = (ImageView) findViewById(R.id.imageAvatar);
		imageAvatar.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				new AlertDialog.Builder(MainActivity.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);
									    //拍照图片默认保存在根目录下的DCIM文件夹以pc.jpg 命名
										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) {

		switch (requestCode) {
		case IMAGE_REQUEST_CODE:
			startPhotoZoom(data.getData());
			break;
		case CAMERA_REQUEST_CODE:
				File tempFile = new File(
						Environment
								.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
								+ "/" + IMAGE_FILE_NAME);
				startPhotoZoom(Uri.fromFile(tempFile));
			break;
		case RESULT_REQUEST_CODE:
			if (data != null) {
				getImageToView(data);
			}
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
		}

	/**
	 * 
	 * @Title:        startPhotoZoom 
	 * @Description:  图片裁剪
	 * @param:        @param uri    
	 * @return:       void    
	 * @throws 
	 * @author        FrankWong
	 * @Date          2013-9-21 上午9:19:12
	 */
	public void startPhotoZoom(Uri uri) {
		Log.d("Test", "startPhotoZoom:" + 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", 320);
		intent.putExtra("outputY", 320);
		intent.putExtra("return-data", true);
		startActivityForResult(intent, RESULT_REQUEST_CODE);
	}

	/**
	 * 保存裁剪之后的图片数据
	 * 
	 * @param picdata
	 */
	private void getImageToView(Intent data) {
		Log.d("Test", "getImageToView:" + data.getExtras().toString());
		Bundle extras = data.getExtras();
		if (extras != null) {
			Bitmap photo = extras.getParcelable("data");
			File tempFile = new File(
					Environment
							.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
							+ "/" + IMAGE_AVATAR);
			BufferedOutputStream bos;
			try {
				bos = new BufferedOutputStream(new FileOutputStream(tempFile));
				photo.compress(Bitmap.CompressFormat.JPEG, 80, bos);
				bos.flush();
				bos.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

			Drawable drawable = new BitmapDrawable(getResources(), photo);
			imageAvatar.setImageDrawable(drawable);
			imagePath = tempFile.getPath();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

源码见附件
avatar.rar

  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.
*