从相册获取图片

android 4.4 从相册选择(兼容各个版本)

	//选择图片
  	Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);  
    startActivityForResult(intent, IMAGE_OPEN);

android 除了4.4版本,其他的都兼容

 Intent  intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "选择图片"), PICTURE);


	/**
	 * 打开相册
	 */
	protected void startSelectPhotos() {
		//选择图片(方式一)
		Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
//		打开相册(方式二)
//      Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
//		intent.addCategory(Intent.CATEGORY_OPENABLE);
		intent.setType("image/*");
		startActivityForResult(Intent.createChooser(intent, "选择图片"), PICTURE);
	}

	/**
	 * 打开相机
	 */
	protected void startActionCamera() {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, getCameraTempFile());
		startActivityForResult(intent, CAMERA);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode != Activity.RESULT_OK) {
			return;
		}
		switch (requestCode) {
		case CAMERA:
			cropImageUri(cropUri, UPLOADPICTURE);
			break;
		case PICTURE:
			cropImageUri(data.getData(), UPLOADPICTURE);
			break;
		case UPLOADPICTURE://上传图片,上传图片成功后,直接获取cropUri来设置图片(imgHead.setImageURI(cropUri);// 回调成功后更新个人中心头像)
			strategy = new Strategy(new ModifyImageImpl(getActivity(),
					imageSaveFile));
			strategy.operate(callBack);
			break;
		default:
			break;
		}
	}

裁剪图片

/**
	 * @param uri
	 * @param outputX
	 * @param outputY
	 * @param requestCode
	 *            裁剪图片
	 */
	private void cropImageUri(Uri uri, int requestCode) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		intent.putExtra("crop", "true");// crop为true是在开启的intent中设置显示的view可裁剪
		intent.putExtra("aspectX", 28);// 设置裁剪的宽、高比例为1:1
		intent.putExtra("aspectY", 33);
		intent.putExtra("outputX", 140);// outputX,outputY是裁剪的宽、高度
		intent.putExtra("outputY", 165);
		intent.putExtra("scale", true);// 是否保留比例
		// if (imageUri == null) {
		// imageUri = Uri.fromFile(imageSaveFile);
		// }
		// intent.putExtra("output", imageUri);
		intent.putExtra("output", this.getUploadTempFile(uri));// this.getUploadTempFile(uri)
		// intent.putExtra(MediaStore.EXTRA_OUTPUT, this.imageUri);// 直接输出文件
		intent.putExtra("return-data", true);// 是否返回数据
		intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
		// intent.putExtra("outputFormat",
		// Bitmap.CompressFormat.JPEG.toString());
		intent.putExtra("noFaceDetection", true); // 关闭人脸检测
		startActivityForResult(intent, requestCode);
	}

拍照或裁剪图片后的保存路径

/**
	 * 拍照保存的绝对路径
	 */
	private Uri getCameraTempFile() {
		String storageState = Environment.getExternalStorageState();
		if (storageState.equals(Environment.MEDIA_MOUNTED)) {
			File savedir = new File(Constant.IMAGE_SAVE_PATH);
			if (!savedir.exists()) {
				savedir.mkdir();
			}
		} else {
			// UIUtils.ToastMessage(getActivity(), "无法保存上传的头像,请检查SD卡是否挂载");
			return null;
		}
		String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA)
				.format(new Date());
		// 照片命名
		String cropFileName = "virtual" + BaseApplication.user.getUserId()
				+ "_" + timeStamp + ".jpg";
		// copyProtraitName = cropFileName;

		// 裁剪头像的绝对路径
		// protraitPath = IMAGE_SAVE_PATH + cropFileName;
		imageSaveFile = new File(Constant.IMAGE_SAVE_PATH, cropFileName);
		cropUri = Uri.fromFile(imageSaveFile);
		return this.cropUri;
	}

	/**
	 * 裁剪头像的绝对路径
	 */
	private Uri getUploadTempFile(Uri uri) {
		String storageState = Environment.getExternalStorageState();
		if (storageState.equals(Environment.MEDIA_MOUNTED)) {
			File savedir = new File(Constant.IMAGE_SAVE_PATH);
			if (!savedir.exists()) {
				savedir.mkdirs();
			}
			// try { //创建的文件需要修改读写权限
			// Process p = Runtime.getRuntime().exec(
			// "chmod 777 " + IMAGE_SAVE_PATH);
			// int status = p.waitFor();
			// if (status == 0) {
			// // chmod succeed
			// ShowToast.getToast().setMessage(getActivity(), "成功").show();
			// } else {
			// // chmod failed
			// }
			// } catch (Exception e) {
			// e.printStackTrace();
			// }
		} else {
			ShowToast.getToast()
					.setMessage(getActivity(), "无法保存上传的头像,请检查SD卡是否挂载").show();
			return null;
		}
		String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA)
				.format(new Date());
		String thePath = ImageUtils.getAbsolutePathFromNoStandardUri(uri);

		// 如果是标准Uri
		if (StringUtils.isEmpty(thePath)) {
			thePath = ImageUtils.getAbsoluteImagePath(getActivity(), uri);
		}
		String ext = AdvancedFileUtils.getFileFormat(thePath);
		ext = StringUtils.isEmpty(ext) ? "jpg" : ext;

		// 照片命名
		String cropFileName = "virtual" + BaseApplication.user.getUserId()
				+ "_" + timeStamp + "." + ext;
		// copyProtraitName = cropFileName;

		// 裁剪头像的绝对路径
		// protraitPath = IMAGE_SAVE_PATH + cropFileName;

		imageSaveFile = new File(Constant.IMAGE_SAVE_PATH, cropFileName);
		cropUri = Uri.fromFile(imageSaveFile);
		return this.cropUri;
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值