调用相机和相册进行裁剪上传头像

最近呢,做了一个简单的上传头像,用dialog弹出框,进行选择是相机还是相册

不知道怎么说,先写布局吧,也可以说是相当简单,但是为了怕有人不会,或者是看不懂,我在这里一部到位



    
	

因为,我写在Fragment里面了,不要问我为什么,不写在Activity里面,因为我懒,不想改。。。。

public class MyFragment extends Fragment implements OnClickListener {

	private ImageView img_view;
	private AlertDialog alertDialog;
	private Button btn_xiangce;
	private Button btn_pai;
	private String path = Environment.getExternalStorageDirectory() + "/head.jpg";

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.my_fragment, null, false);
		img_view = (ImageView) view.findViewById(R.id.img_view);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		img_view.setOnClickListener(this);
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.img_view:
			createDialog();
			break;
		case R.id.btn_pai:
			Intent PIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			PIntent.putExtra(MediaStore.EXTRA_OUTPUT,
					Uri.fromFile(new File(path)));
			startActivityForResult(PIntent, 100);
			alertDialog.dismiss();
			break;
		case R.id.btn_xiangce:
			Intent XIntent = new Intent(Intent.ACTION_PICK);
			XIntent.setType("image/*");
			startActivityForResult(XIntent, 99);
			alertDialog.dismiss();
			break;
		default:
			break;
		}
	}

	private void createDialog() {
		// TODO Auto-generated method stub
		Builder builder = new AlertDialog.Builder(getActivity());
		alertDialog = builder.create();
		alertDialog.setTitle("请选择");
		View dialogView = View.inflate(getActivity(), R.layout.btn_layout,
				null);
		alertDialog.setView(dialogView);
		btn_xiangce = (Button) dialogView.findViewById(R.id.btn_xiangce);
		btn_pai = (Button) dialogView.findViewById(R.id.btn_pai);
		btn_xiangce.setOnClickListener(this);
		btn_pai.setOnClickListener(this);
		alertDialog.show();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 99 && resultCode == getActivity().RESULT_OK) {
			// 裁剪
			crop(data.getData());
		}
		if (requestCode == 89 && resultCode == getActivity().RESULT_OK) {
			img_view.setImageBitmap((Bitmap) data.getParcelableExtra("data"));
		}
		if (requestCode == 100 && resultCode == getActivity().RESULT_OK) {
			crop(Uri.fromFile(new File(path)));
		}
	}

	private void crop(Uri data) {
		// TODO Auto-generated method stub
		Intent cIntent = new Intent("com.android.camera.action.CROP");
		cIntent.setDataAndType(data, "image/*");
		cIntent.putExtra("crop", "true");
		cIntent.putExtra("aspectX", 1);
		cIntent.putExtra("aspectY", 1);
		cIntent.putExtra("outputX", 249);
		cIntent.putExtra("outputY", 249);
		cIntent.putExtra("return-data", true);
		startActivityForResult(cIntent, 89);
	}
}

在这里声明一下,Dialog导包问题,因为Android studio的导包没有Eclipse好,为了避免有人,导错包....

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;

最后一个是Dialog个的布局,我在这里一部到位了




    

权限声明,别忘了,否则拿不到本地图片


 

你可能感兴趣的:(学习)