tags:

views:

35

answers:

1

Is there an Image cropping Activity in Android? I know that when you save an images as your wallpaper, it pops up an image cropper... and I've looked into the sourcecode for that, but it depends on a LOT of gallery specific stuff. Not very reusable.

Does anyone know how this might be done, or are there any third party libraries that can help me out on this one?

+1  A: 

There's a reasonably well-supported Intent for this (worked on my testing on at least a half dozen different Android 2.1+ phones, including SenseUI and Motoblur devices, and I believe it's been around since Android 1.0 or earlier on the G1). This might get you started:

    final Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setData(uriOfImageToCrop);
    intent.putExtra("outputX", 400);
    intent.putExtra("outputY", 400);
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("scale", true);
    intent.putExtra("noFaceDetection", true);
    intent.putExtra("output", Uri.fromFile(someOutputFile));
    startActivityForResult(intent, SOME_RANDOM_REQUEST_CODE);

Then just handle what you need to do in the onActivityResult() method of your Activity; your output file should have the cropped image in it at that point.

To be safe, though, you may want to have a fallback behavior (autocrop? Don't crop?) if someone has a device that doesn't support this Intent.

Yoni Samlan
How can I tell if this Intent is supported?
synic
Also, are you sure that's the intent name? I get the following error on the Droid and the emulator: No Activity found to handle Intent { act=com.android.camera.action.CROP dat=file:///mnt/sdcard/data/okcupid/mediacache/takephoto (has extras) }
synic
See this Romain Guy blog post on checking if there's an intent available: http://android-developers.blogspot.com/2009/01/can-i-use-this-intent.html As far as why you're getting the error, it may not be recognizing the format of the URL you're passing it... I always operate directly on a URI returned in the data portion of an Intent from an ACTION_PICK or ACTION_IMAGE_CAPTURE, which I believe comes back with a "media://" URI. You also may want to check some other documentation, like http://www.androidworks.com/crop_large_photos_with_android
Yoni Samlan
And of course, the Android AOSP source code for the Camera app, including its manifest, which should tell you exactly what intent-filters are set up for this on default Android devices.
Yoni Samlan