// variables we need for Android7 image crop
private static final int CODE_PHOTO = 1115;
private static final int CODE_RESULT = 1118;
private static final int CODE_CANCEL = 5556;
String path = Environment.getExternalStorageDirectory()+"/images/";
File mCameraFile = new File(path, "photo.jpg");//照相机的File对象
File mCropFile = new File(path, "avatar.jpg");//裁剪后的File对象
/**
* Basic steps for setting avatar with crop image
* Step 1: Start a intent for PHOTO, this step should be different in android7
* Step 2: onActivityResult received photo request, start zoom image
* Step 3: in startZoomImage it will using gallery to crop image, and then send result code
* Step 4: in onActivityResult method received the cropped image and set it.
*/
public void chooseAvatar() {
// check parent dir is exist or not
if (!mCameraFile.getParentFile().exists()) {
mCameraFile.getParentFile().mkdirs();
}
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent, CODE_PHOTO);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case CODE_CANCEL:
Toast.makeText(getActivity(), "取消了相册选择", Toast.LENGTH_SHORT).show();
case CODE_PHOTO:
if (data != null) {
Uri uri = data.getData();
DebugHelper.LogMessage("step 2, start zoom image", "crop and choose photo");
startPhotoZoom(uri);
}
break;
case CODE_RESULT:{
Uri inputUri = FileProvider.getUriForFile(getActivity(), "com.ouman.luoliluoli.fileprovider", mCropFile);
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(inputUri));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
DebugHelper.LogMessage("step 4, get result and set cropped image.", "crop and choose photo");
avatar.setImageBitmap(bitmap);
avatarImageFile = mCropFile;
break;
}
}
}
public void startPhotoZoom(Uri inputUri) {
if (inputUri == null) {
Log.e("error","The uri is not exist.");
return;
}
Intent intent = new Intent("com.android.camera.action.CROP");
//sdk>=24
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri outPutUri = Uri.fromFile(mCropFile);
intent.setDataAndType(inputUri, "image/*");
intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutUri);
intent.addFlags(FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(FLAG_GRANT_WRITE_URI_PERMISSION);
} else {
Uri outPutUri = Uri.fromFile(mCropFile);
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
//这个方法是处理4.4以上图片返回的Uri对象不同的处理方法
String url = FileHelper.getRealPathFromURI(getActivity(), inputUri);
intent.setDataAndType(Uri.fromFile(new File(url)), "image/*");
} else {
intent.setDataAndType(inputUri, "image/*");
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutUri);
}
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 250);
intent.putExtra("outputY", 250);
intent.putExtra("return-data", false);
intent.putExtra("noFaceDetection", false);
intent.putExtra("outputFormat", "JPEG");
DebugHelper.LogMessage("step 3, start return result back", "crop and choose photo");
startActivityForResult(intent, CODE_RESULT);
}