Получите два requestCode == UCrop.REQUEST_CROP в onActivityResult?
Я использую библиотеку uCrop, чтобы выполнить обрезку изображения.
Мне нужно вырезать два разных изображения в одном упражнении. Но с кодом, который у меня есть, возврат падает в один цикл.
Как я мог отделить это возвращение, чтобы относиться к нему самостоятельно?
Код:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == UCrop.RESULT_ERROR) {
Toast.makeText(this, "uCrop error", Toast.LENGTH_SHORT).show();
return;
}
if (requestCode == UCrop.REQUEST_CROP) {
mSelectImage.setImageResource(0);
final Uri imgUri = UCrop.getOutput(data);
mResultUri = imgUri;
mSelectImage.setImageURI(mResultUri);
temDadosSalvar = true;
Toast.makeText(this, "Cortada", Toast.LENGTH_SHORT).show();
return;
}
if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
mImageUri = data.getData();
if (mImageUri == null) {
mProgress.dismiss();
return;
} else {
File tempCropped = new File(getCacheDir(), "tempImgCropped.png");
Uri destinationUri = Uri.fromFile(tempCropped);
UCrop uCrop = UCrop.of(mImageUri, destinationUri);
uCrop = advancedConfig(uCrop);
uCrop.start(this);
}
}
if (requestCode == CAMERA_REQUEST_CODE_CAPA && resultCode == RESULT_OK) {
mImageUri = data.getData();
if (mImageUri == null) {
mProgress.dismiss();
return;
} else {
File tempCropped = new File(getCacheDir(), "tempImgCropped.png");
Uri destinationUri = Uri.fromFile(tempCropped);
UCrop uCrop = UCrop.of(mImageUri, destinationUri);
uCrop = advancedConfig(uCrop);
uCrop.start(this);
}
}
}
}
1 ответ
Решение
Вы можете создать две переменные класса, чтобы определить, какое изображение находится в процессе обрезки.
private Uri mDestinationCapa;
private Uri mDestinationCamera;
if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
mImageUri = data.getData();
if (mImageUri == null) {
...
} else {
File tempCropped = new File(getCacheDir(), "tempImgCropped.png");
mDestinationCamera= Uri.fromFile(tempCropped);
...
}
}
if (requestCode == CAMERA_REQUEST_CODE_CAPA && resultCode == RESULT_OK) {
mImageUri = data.getData();
if (mImageUri == null) {
...
} else {
File tempCropped = new File(getCacheDir(), "tempImgCropped.png");
mDestinationCapa = Uri.fromFile(tempCropped);
...
}
}
На ActivityResult вы можете сравнить результат Uri с локальным Uris:
if (requestCode == UCrop.REQUEST_CROP) {
final Uri imgUri = UCrop.getOutput(data);
if(mDestinationCamera != null && imgUri.equals(mDestinationCamera)){
//save camera cropped image
}if(mDestinationCamera != null && imgUri.equals(mDestinationCamera)){
//save capa cropped image
}
return;
}