Android调用安卓相机拍照上传

popupWindow_photo = new PopupWindow_photo(getActivity().getApplicationContext(), base_title);


/* bitmap截图信息 */

	// 修改头像
	/*
	 * 
	 * public class headrel implements View.OnClickListener {
	 * 
	 * @Override public void onClick(View v) { popupWindow_photo = new
	 * PopupWindow_photo(getActivity(), base_title); } }
	 */

	// 选择头像弹窗
	private static final int PHOTO_REQUEST_CAMERA = 1;// 拍照
	private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
	private static final int PHOTO_REQUEST_CUT = 3;// 结果

	public class PopupWindow_photo extends PopupWindow {
		private Button select_camera;
		private Button select_photo;
		private Button select_out;
		private LinearLayout select_input;

		public PopupWindow_photo(Context mContext, View parent) {
			super(parent);
			View view = View
					.inflate(mContext, R.layout.popup_type_select, null);// 自己写的xml弹框布局
			setWidth(ViewGroup.LayoutParams.MATCH_PARENT);// 设置宽度
			setHeight(ViewGroup.LayoutParams.MATCH_PARENT);// 设置高度
			setFocusable(true);
			setOutsideTouchable(true);// 设置是否可以外部点击
			setContentView(view);// 在父view中呈现
			showAtLocation(parent, Gravity.TOP, 0, 0);// 在父view的x,y位置出现
			update();// 刷新
			// 自己xml定义的组件用法
			select_input = (LinearLayout) view.findViewById(R.id.select_input);
			select_camera = (Button) view.findViewById(R.id.select_camera);
			select_photo = (Button) view.findViewById(R.id.select_photo);
			select_out = (Button) view.findViewById(R.id.select_out);
			StaticData.buttonnowscale(select_camera, 90, 610);
			StaticData.buttonnowscale(select_photo, 90, 610);
			StaticData.buttonnowscale(select_out, 90, 610);
			StaticData.linearlayoutnowscale(select_input, 275, 610);
			select_camera.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(
							"android.media.action.IMAGE_CAPTURE");
					// 判断存储卡是否可以用,可用进行存储
					if (hasSdcard()) {
						intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri
								.fromFile(new File(Environment
										.getExternalStorageDirectory(),
										"IMGface")));
					}
					startActivityForResult(intent, PHOTO_REQUEST_CAMERA);
					dismiss();
				}
			});
			select_photo.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(Intent.ACTION_PICK);
					intent.setType("image/*");
					startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
					dismiss();
				}
			});
			select_out.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					dismiss();
				}
			});
		}
	}

	@SuppressLint("ShowToast")
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case PHOTO_REQUEST_GALLERY:
			if (data != null) {
				// 得到图片的全路径
				Uri uri = data.getData();
				crop(uri);
			}
			break;
		case PHOTO_REQUEST_CAMERA:
			if (hasSdcard()) {
				tempFile = new File(Environment.getExternalStorageDirectory(),
						"IMGface");
				crop(Uri.fromFile(tempFile));
			} else {
				Toast.makeText(getActivity().getApplicationContext(),
						"未找到存储卡,无法存储照片!", 0).show();
			}
			break;
		case PHOTO_REQUEST_CUT:

			bitmap = data.getParcelableExtra("data");

			imgs.setImageBitmap(bitmap);
			try {
				if (bitmap != null) {
					// boolean delete = tempFile.delete();
					// System.out.println("delete = " + delete);
					f = new File(getActivity().getApplicationContext()
							.getFilesDir(), "IMGface.jpg");
					FileOutputStream out = null;
					out = new FileOutputStream(f);
					bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
					out.flush();
					out.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

			sendData();

			break;

		}
	}

	/**
	 * 剪切图片
	 * 
	 * @param uri
	 * @function:
	 * @author:Jerry
	 * @date:2013-12-30
	 */
	private void crop(Uri uri) {
		// 裁剪图片意图
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		// 裁剪框的比例,1:1
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// 裁剪后输出图片的尺寸大小
		intent.putExtra("outputX", 250);
		intent.putExtra("outputY", 250);
		// 图片格式
		intent.putExtra("outputFormat", "JPG");
		intent.putExtra("noFaceDetection", true);// 取消人脸识别
		intent.putExtra("return-data", true);// true:不返回uri,false:返回uri
		startActivityForResult(intent, PHOTO_REQUEST_CUT);

	}

	private boolean hasSdcard() {
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			return true;
		} else {
			return false;
		}
	}

	// 上传
	public void sendData() {

		String url = UrlVO.Host_Url + UrlVO.Change_Url;
		try {
			RequestParams params = new RequestParams();
			params.put("file", f);

			AsyncHttpClient client = new AsyncHttpClient();
			if (!UrlVO.getShareData("JSESSIONID",
					getActivity().getApplicationContext()).equals("false")) {
				client.addHeader("Cookie", UrlVO.getShareData("JSESSIONID",
						getActivity().getApplicationContext()));
			}
			client.post(url, params, new AsyncHttpResponseHandler() {
				@Override
				public void onSuccess(String response) {
					Bundle bundle = new Bundle();
					bundle.putString("response", response);
					if (response == null || response.equals("")) {
						return;
					}
					String[] key = { "success" };
					String[] logindata = json.getJSON(response, key);
					if (logindata == null) {
						return;
					}
					if (logindata[0].equals("true")) {
						Toast.makeText(getActivity().getApplicationContext(),
								"修改成功", 3000).show();
						// getActivity().finish();
						getData(UrlVO.Personal_Url, UrlVO.JSESSIONID);// 获取个人
					}
				}

				@Override
				public void onFailure(int statusCode,
						org.apache.http.Header[] headers, byte[] responseBody,
						Throwable error) {
					// TODO 自动生成的方法存根
					super.onFailure(statusCode, headers, responseBody, error);
					Toast.makeText(getActivity().getApplicationContext(),
							"网络访问异常 ", 0).show();
				}
				// @Override
				// public void onFailure(int statusCode, Header[] headers,
				// byte[] responseBody, Throwable error) {
				// super.onFailure(statusCode, headers, responseBody, error);
				// Toast.makeText(getActivity().getApplicationContext(),
				// "网络访问异常 ", 0).show();
				// }

			});

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值