Reputation: 308
In my app there is an activity taking a picture from camera, saving it and displaying on ImageView followingby. I use FilePath class to point where to store it on a path and CameraUtils class to get file:URI and convert the image to BitMap. Now my task is to add a crop functionality to get rid of areas that are out of our interests. The crop function should activate on the screen just after a picture taken and then cropped photo need to be saved to display. Before that please see the working codes before crop added:
I have this method to capture an image:
/* Capture Image Method */
private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//Start intent with Action_Image_Capture
fileUri = CameraUtils.getOutputMediaFileUri(this);//get fileUri from CameraUtils
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);//Send fileUri with intent
//intent.putExtra("return-data",true); // Should be for crop
startActivityForResult(intent, CAMERA_REQUEST_CODE);//start activity for result with CAMERA_REQUEST_CODE
}
This method to show captured image
/* Show Captured over ImageView */
private void showCapturedImage() {
if (!getImageUrl.equals("") && getImageUrl != null)
imageView.setImageBitmap(CameraUtils.convertImagePathToBitmap(getImageUrl, false));
else
Toast.makeText(this, R.string.capture_image_failed, Toast.LENGTH_SHORT).show();
}
The method to crop image:
private void CropImage() {
try {
Intent CropIntent = new Intent("com.android.camera.action.CROP");
CropIntent.setDataAndType(uri, "image/*");
CropIntent.putExtra("crop", "true");
CropIntent.putExtra("outputX", 180);
CropIntent.putExtra("outputY", 180);
CropIntent.putExtra("aspectX", 3);
CropIntent.putExtra("aspectY", 4);
CropIntent.putExtra("scaleUpIfNeeded", true);
CropIntent.putExtra("return-data", true);
startActivityForResult(CropIntent, 1003);
} catch (ActivityNotFoundException ex) {
//
}
}
onActivityResult() method:
/**
* public static final int PERMISSION_REQUEST_CODE = 1001;//request code for Camera and External Storage permission
* private static final int CAMERA_REQUEST_CODE = 1002;//request code for capture image
* private static final int CROP_REQUEST_CODE = 1003;//request code for crop
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CAMERA_REQUEST_CODE:
try {
//When image is captured successfully
if (resultCode == RESULT_OK) {
//Check if device SDK is greater than 22 then we get the actual image path via below method
if (Build.VERSION.SDK_INT > 22)
getImageUrl = FilePath.getPath(MainActivity.this, fileUri);
else
//else we will get path directly
getImageUrl = fileUri.getPath();
//After image capture show captured image over image view
showCapturedImage();
} else
Toast.makeText(this, R.string.cancel_message, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
Till this point everthing works very well but when it comes to inserting the crop function, I'm struggling with adding if else/statements with required requestCodes into switch/case statements as it throws an unknown errors. Can you help me with the creating the proper logic with multiple requstCodes and make it work, please?
Upvotes: 0
Views: 5072
Reputation: 308
After making the following changes I could make it working.
First of all CropIntent was passing value through key-pair values which was supposed to pass through MediaStore.EXTRA_OUTPUT and the uri was not defined with correct name.
private void CropImage() {
try {
Intent CropIntent = new Intent("com.android.camera.action.CROP");
CropIntent.setDataAndType(fileUri, "image/*");
fileUri = CameraUtils.getOutputMediaFileUri(this);
CropIntent.putExtra("crop", "true");
CropIntent.putExtra("outputX", 180);
CropIntent.putExtra("outputY", 180);
CropIntent.putExtra("aspectX", 3);
CropIntent.putExtra("aspectY", 4);
CropIntent.putExtra("scaleUpIfNeeded", true);
CropIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(CropIntent, CROP_REQUEST_CODE);
} catch (ActivityNotFoundException ex) {
//
}
}
Second, I replaced switch/case statement with if/else in onActivityResult()
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
if (resultCode == RESULT_OK)
CropImage();
} else if (requestCode == CROP_REQUEST_CODE) {
//Check if device SDK is greater than 22 then we get the actual image path via below method
if (Build.VERSION.SDK_INT > 22)
getImageUrl = FilePath.getPath(MainActivity.this, fileUri);
else
//else we will get path directly
getImageUrl = fileUri.getPath();
//After image capture show captured image over image view
showCapturedImage();
}
}
Upvotes: 1