user724861
user724861

Reputation: 1074

Fail Image cropping in Android

so I want to choose an image from gallery and then crop it:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
      "Select Picture"), PHOTO_PICKED_WITH_DATA);

OK, pick the photo and then catch it onActivityResult, then crop it:

Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(mAvatarUri, "image/*");
    intent.putExtra("crop", true);
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("outputX", ICON_SIZE);
    intent.putExtra("outputY", ICON_SIZE);
    intent.putExtra("scale", true);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mAvatarUri);
    intent.putExtra("return-data", true);
    startActivityForResult(intent, CROP_IMAGE);

now, the problem is when i want to transform it into bytes and then send it inside an xml... it doesn't take only the cropped image but instead the whole image itself... also, i cannot access cropped image uri, it says file not found! hmmmm, seems like my cropped image weren't saved after all...

how can i suppose to fix this?

Samsung ACE 2.3.4

Upvotes: 0

Views: 1431

Answers (2)

deepa
deepa

Reputation: 2494

Check this code in the following link.

Crop Image

It works fine for me..

Upvotes: 1

ariefbayu
ariefbayu

Reputation: 21979

I don't know how you got that technique to crop image. But, for me, I always use this library. And it never failed to impress me. Work from android 2.1 all the way to 3.2 (never test it on 4.0 onward).

Here's how I do it:

Intent cropIntent = new Intent(imageProcessActivity,
        CropImage.class);
cropIntent.putExtra("image-path",
        FileUtil.saveTempFile(ImageProcessActivity.processedBitmap, filename));
cropIntent.putExtra("scale", true);
imageProcessActivity.startActivityForResult(cropIntent, ImageProcessActivity.INTENT_CROP_CODE);

and here's how to catch the result:

if (requestCode == INTENT_CROP_CODE && resultCode == RESULT_OK) {
    Bundle extras = intent.getExtras();
    if (extras != null) {
        Uri uri = null;
        uri = (Uri) extras.get("imageCrop");
        Bitmap bitmap = null;
        try {
            bitmap = ImageUtil.decodeFile(
                    new File(new URI(uri.toString())),
                    AppConstant.MAX_IMAGE_SIZE);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        processedBitmap = bitmap;
        selectedImage.setImageBitmap(bitmap);
    }
}

Upvotes: 0

Related Questions