1、打开相机方法
public static String SAVED_IMAGE_DIR_PATH = Environment.getExternalStorageDirectory().getPath() + "/wxt/camera/";
String path=SAVED_IMAGE_DIR_PATH + System.currentTimeMillis() + ".jpg";
public void openCarama(int path) {
try {
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
String out_file_path = SAVED_IMAGE_DIR_PATH;
File dir = new File(out_file_path);
if (!dir.exists()) {
dir.mkdirs();
}
Uri uri = null;
uri = Uri.fromFile(new File(path));
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
getActivity().startActivityForResult(intent, mType);
} else {
CommonUtil.toast("请确认已经插入SD卡", getActivity());
}
} catch (Exception e) {
CommonUtil.toast("请您确认是否打开相机权限", getActivity());
e.printStackTrace();
}
}
2、剪切图片的方法
private void cropImageUri(Uri uri, int outputX, int outputY, int requestCode) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// 设置x,y的比例,截图方框就按照这个比例来截 若设置为0,0,或者不设置 则自由比例截图
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 0);//截图的比例 outputX outputY,为0的时候,就可以任意了
intent.putExtra("outputY", 0);
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
mPathinHandCrop = SAVED_IMAGE_DIR_PATH + System.currentTimeMillis() + ".jpg";
File file = new File(mPathinHandCrop);
intent.putExtra("output", Uri.fromFile(file));
intent.putExtra("return-data", true);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true); // no face detection
startActivityForResult(intent, requestCode);
}
3.在onActivityResult里获取图片
@Override
public void onFragmentResult(int requestCode, int resultCode, Intent data) {
super.onFragmentResult(requestCode, resultCode, data);
switch (requestCode) {
case PERSON_PICTURE_FONT://打开相机的requestcode
// 把身份证正面保存到本地
File mFrontFile = new File(path);
if (mFrontFile.exists()) {
try {
//压缩图片方法 CommunityMessageCacaheManager.getInstance().comppressPhoto(mFrontFile.getAbsolutePath(), new CommunityMessageCacaheManager.OnPhotoPressFinishListner() {
@Override
public void onFinish(final String filePath) {
path= filePath;
mFrontFile = new File(path);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
break;
case Constant.PERSON_PICTURE_INHAND://获取剪切的图片
mPathinHandCrop= new File(mPathinHandCrop);
if (mPathinHandCrop.exists()) {
try {
CommunityMessageCacaheManager.getInstance().comppressPhoto(mPathinHandCrop.getAbsolutePath(), new CommunityMessageCacaheManager.OnPhotoPressFinishListner() {
@Override
public void onFinish(final String filePath) {
mPathinHandCrop= filePath;
mInhandFile = new File(mPathinHandCrop);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
break;
}
这里的截图更加完整,可以查看一下
剪切图片的各个方法