1
Intent intent = new Intent("com.android.camera.action.CROP");

File path = this.getExternalFilesDir("tmp");
File file = new File(path, "tmp_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
Uri tmpUri = Uri.fromFile(file);

intent.setData(selectedImage);
intent.putExtra(MediaStore.EXTRA_OUTPUT, tmpUri);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("crop", "true");
intent.putExtra("scale", "true");
intent.putExtra("outputX", 100);
intent.putExtra("outputY", 100);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);

intent.putExtra("return-data", false);        
startActivityForResult(intent, REQUEST_CROP);

I am using this code to crop image. It works perfectly on android 2.x. But on 3.1(motorola xoom) and 3.2(acer iconia) the application freeze after I select crop area and tap "Save" (onActivityResult is not even called). There is a real Uri to real image in selectedImage variable, so the problem is not here.

On the 3.1 and 3.2 android emulators the application works perfectly too. Does anyone know what's the problem?

Skiff_
  • 11
  • 2

3 Answers3

2

Try this method, it worked for me. It's not mine, I've founded here in stackoverflow: How to crop Bitmap Center like imageview?

public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
    int sourceWidth = source.getWidth();
    int sourceHeight = source.getHeight();

    // Compute the scaling factors to fit the new height and width, respectively.
    // To cover the final image, the final scaling will be the bigger 
    // of these two.
    float xScale = (float) newWidth / sourceWidth;
    float yScale = (float) newHeight / sourceHeight;
    float scale = Math.max(xScale, yScale);

    // Now get the size of the source bitmap when scaled
    float scaledWidth = scale * sourceWidth;
    float scaledHeight = scale * sourceHeight;

    // Let's find out the upper left coordinates if the scaled bitmap
    // should be centered in the new size give by the parameters
    float left = (newWidth - scaledWidth) / 2;
    float top = (newHeight - scaledHeight) / 2;

    // The target rectangle for the new, scaled version of the source bitmap will now
    // be
    RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);

    // Finally, we create a new bitmap of the specified size and draw our new,
    // scaled bitmap onto it.
    Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
    Canvas canvas = new Canvas(dest);
    canvas.drawBitmap(source, null, targetRect, null);

    return dest;
}
Community
  • 1
  • 1
Jon Zangitu
  • 957
  • 1
  • 15
  • 30
2

The problem is probably the crop activity that is started. The "crop" = "true" extra is not guaranteed to work on all devices since it is unofficial.

See this thread for more details: http://groups.google.com/group/android-developers/browse_frm/thread/2dd647523926192c/4b6d087073a39607?tvc=1#4b6d087073a39607

Display name
  • 2,697
  • 2
  • 31
  • 49
-1

Try replacing intent.setData(selectedImage); by intent.setDataAndType(selectedImage, "image/*");

Guilherme
  • 7,839
  • 9
  • 56
  • 99