Monday, 12 May 2014

Crop image using intent in android


Crop image using intent in android

          This is the sample program for crop the selected image using intent in startActivityResult in android.

Follow the simple step to crop the selected image.

1)StartActivityResult for pick the photo from the Camera or from file location.



Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
    "tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));

    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

    try {
    intent.putExtra("return-data", true);
    startActivityForResult(intent, PICK_FROM_CAMERA);
    } catch (ActivityNotFoundException e) {
    e.printStackTrace();
    }
    } else { //pick from file
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);


      

    2)After get the photo ,then StartActivityResult for crop the selected image.using ,


    final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setType("image/*");
    List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
    int size = list.size();
    if (size == 0) {
    Toast.makeText(this"Can not find image crop app", Toast.LENGTH_SHORT).show();
    return;
    } else {
    intent.setData(mImageCaptureUri);
    intent.putExtra("outputX", 400);
    intent.putExtra("outputY", 400);
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("scale"true);
    intent.putExtra("return-data"true);
    if (size == 1) {
    Intent i = new Intent(intent);
    ResolveInfo res = list.get(0);
    i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));


    startActivityForResult(i, CROP_FROM_CAMERA);




    3)That will return the croped image .get the crop image and make use of it.


    Bundle extras = data.getExtras();
    if (extras != null) {
    Bitmap photo = extras.getParcelable("data");
    mImageView.setImageBitmap(photo);



    Screenshots:



    Download full source code..



    1 comment:

    Unknown said...

    tq so much,
    keep it up
    :)