0

I am using the following code to allow user to capture Image and then crop it in android, I have found the code on internet

public class ShootAndCropActivity extends Activity implements OnClickListener {

//keep track of camera capture intent
final int CAMERA_CAPTURE = 1;
//keep track of cropping intent
final int PIC_CROP = 2;
//captured picture uri
private Uri picUri;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //retrieve a reference to the UI button
    Button captureBtn = (Button)findViewById(R.id.capture_btn);
    //handle button clicks
    captureBtn.setOnClickListener(this);
}

/**
 * Click method to handle user pressing button to launch camera
 */
public void onClick(View v) {
    if (v.getId() == R.id.capture_btn) {     
        try {
            //use standard intent to capture an image
            Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //we will handle the returned data in onActivityResult
            startActivityForResult(captureIntent, CAMERA_CAPTURE);
        }
        catch(ActivityNotFoundException anfe){
            //display an error message
            String errorMessage = "Whoops - your device doesn't support capturing images!";
            Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }
}

/**
 * Handle user returning from both capturing and cropping the image
 */
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        //user is returning from capturing an image using the camera
        if(requestCode == CAMERA_CAPTURE){
            //get the Uri for the captured image
            picUri = data.getData();
            //carry out the crop operation
            performCrop();
        }
        //user is returning from cropping the image
        else if(requestCode == PIC_CROP){
            //get the returned data
            Bundle extras = data.getExtras();
            //get the cropped bitmap
            Bitmap thePic = extras.getParcelable("data");
            //retrieve a reference to the ImageView
            ImageView picView = (ImageView)findViewById(R.id.picture);
            //display the returned cropped image
            picView.setImageBitmap(thePic);
        }
    }
}

/**
 * Helper method to carry out crop operation
 */
private void performCrop(){
    //take care of exceptions
    try {
        //call the standard crop action intent (the user device may not support it)
        Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
        //indicate image type and Uri
        cropIntent.setDataAndType(picUri, "image/*");
        //set crop properties
        cropIntent.putExtra("crop", "true");
        //indicate aspect of desired crop
        cropIntent.putExtra("aspectX", 1);
        cropIntent.putExtra("aspectY", 1);
        //indicate output X and Y
        cropIntent.putExtra("outputX", 256);
        cropIntent.putExtra("outputY", 256);
        //retrieve data on return
        cropIntent.putExtra("return-data", true);
        //start the activity - we handle returning in onActivityResult
        startActivityForResult(cropIntent, PIC_CROP);  
    }
    //respond to users whose devices do not support the crop action
    catch(ActivityNotFoundException anfe){
        //display an error message
        String errorMessage = "Whoops - your device doesn't support the crop action!";
        Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
        toast.show();
    }
}
}

it is working very nice, but the problem that when it call crop intent it show two options and allow user to choose between them

What I want to do, is to select the first option by default and don't show this selector to user to force him to crop the image, or at least not allow user to close this dialog by press back to force him to crop images.

Can any one help in this ?

Amira Elsayed Ismail
  • 9,216
  • 30
  • 92
  • 175
  • [Android does not have a `CROP` `Intent`](https://commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html). There are many [image cropping libraries available for Android](https://android-arsenal.com/tag/45). Please use one. – CommonsWare Aug 25 '15 at 12:57
  • @CommonsWare but it works with me and open dialog box to choose which crop intent I want to use and it crop image. – Amira Elsayed Ismail Aug 25 '15 at 12:58
  • "but it works with me" -- so? There are over 1 billion Android devices in use. Not all will support a `CROP` `Intent`. Talented developers read documentation and notice that there is nothing in the Android SDK related to this `Intent`. Talented developers will then realize that there is no requirement for all Android devices to support a `CROP` `Intent`. Talented developers will then use a library, rather than rely upon undocumented, unsupported, and frequently non-existent third-party apps for cropping. – CommonsWare Aug 25 '15 at 13:01

0 Answers0