7

I'm working on an Android 2.1+ app and trying to take a photo and then send the results of that intent to a crop function. It seems to work on the Sony Ericsson xmp, but when i put it on a Moto Defy, the crop function fails because it is seemingly ignoring the file name i put in for the data, and instead looks at /data/data/com.motorola.gallery/files/temp-wallpaper for some reason. This has the result of the crop function not returning with any data, and the image i just took gets set as the wallpaper image of the phone! Here's some sample code:

public static final String SD_CARD_TEMP_DIR = Environment.getExternalStorageDirectory() + File.separator;
public static final String SD_CARD_TEMP_ORIG = SD_CARD_TEMP_DIR + "origPhoto.jpg";
public static final String SD_CARD_TEMP_CROP = SD_CARD_TEMP_DIR + "croppedPhoto.jpg";

Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(Uri.fromFile(new File(Const.SD_CARD_TEMP_ORIG)), "image/*");
intent.putExtra("crop", true);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 512);
intent.putExtra("outputY", 512);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Const.SD_CARD_TEMP_CROP)));

The output from this is something like this:

INFO/ActivityManager(1242): Starting activity: Intent { act=com.android.camera.action.CROP dat=file:///sdcard/origPhoto.jpg typ=image/* cmp=com.motorola.gallery/.CropImage (has extras) }
INFO/ActivityManager(1242): Start proc com.motorola.gallery:CropImage for activity com.motorola.gallery/.CropImage: pid=25733 uid=10014 gids={1015, 9003, 9007, 9008, 2001, 3003}
WARN/CropImage(25733): CropImg mBitmap was null, retreive frm URIfile:///sdcard/origPhoto.jpg
ERROR/CropImage(25733): got IOException java.io.FileNotFoundException: /data/data/com.motorola.gallery/files/temp-wallpaper 
INFO/ActivityManager(1242): Displayed activity com.motorola.gallery/.CropImage: 5664 ms (total 5664 ms)
WARN/System.err(25672): java.io.FileNotFoundException: /sdcard/croppedPhoto.jpg
DEBUG/(25672): unable to unlink '/sdcard/croppedPhoto.jpg': No such file or directory (errno=2)

Does anyone have any experience with this issue, or ideas for a work around?

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
Elroid
  • 476
  • 4
  • 12
  • 1
    @Elrond: The `com.android.camera.action.CROP` `Intent` action is not part of the Android SDK. There is no guarantee that it will work on any device. – CommonsWare Apr 18 '11 at 22:36
  • I had come across this point elsewhere, which is why i wanted to know if there was alternatively some work around? – Elroid Apr 19 '11 at 10:38
  • Write yourself an image-cropping activity. Or, see if somebody already has written one that is available as open source. – CommonsWare Apr 19 '11 at 13:04
  • Related: [Cropping Picture Issue on Droid X](http://stackoverflow.com/q/3701530) – blahdiblah Mar 05 '13 at 03:32

1 Answers1

3

I'd stay away from the Intent you're using, because it isn't standard, so may not be supported everywhere,

From the extra-data you're using, I believe you're doing a fixed-size crop, and don't need any user input. This makes the solution quite simple, as long as you don't run out of memory.

  1. Load the image via BitmapFactory.decodeFile. You can pass in a BitmapFactory.options object to optionally scale the image at load time.
  2. Create a cropped bitmap with Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height)
  3. Write it out to disk with Bitmap.compress
Phil Lello
  • 8,377
  • 2
  • 25
  • 34
  • 2
    It is a fixed-size crop, but that doesn't mean i don't need user input. The user is asked to position and/or resize the crop region in the appropriate place on the image. For what it's worth i ended up using the solution outlined on http://www.walletapp.net/crop-image which works a treat! – Elroid Apr 27 '11 at 13:02
  • @Elroid That link's rotted, but is still available via Wayback Machine: http://web.archive.org/web/20120219104844/http://www.walletapp.net/crop-image which links to this github repo: https://github.com/biokys/cropimage – blahdiblah Mar 05 '13 at 03:35