0

I need to crop an image from gallery and save to the server. This is my code for cropping image

   private void startCropImage() {

    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(mImageCaptureUri, "image/*");

    intent.putExtra("crop", "true");
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
        //indicate output X and Y
    intent.putExtra("outputX", 400);
    intent.putExtra("outputY", 487);

    intent.putExtra("return-data", true);
    intent.putExtra("scale", true);
    startActivityForResult(intent, CROP_FROM_GALLERY);
}

I need the output in 400*487,but after cropping the image when i check the width it's only 160 on device, but on AVD it shows the correct width. why does it happen? The following is my onActivityResult code

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bundle extras2 = data.getExtras();
if (extras2 != null) {

Bitmap bm2 = extras2.getParcelable("data");

imgview.setImageBitmap(bm2);

int width = bm2.getWidth();}
Deepu T
  • 724
  • 7
  • 20

2 Answers2

0

Check this line:

Bitmap bm2 = extras2.getParcelable("data");

Intents can only transfer limited ammount of data. Depending upon the version or device it might range from 200kb to 1 mb. So this line is returning a thumbnail of cropped image. To get full image do the following. Remember you cannot get the exact width you want.

Uri selectedImage = data.getData();
        String[] filePath = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(
                           selectedImage, filePath, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String filePath = cursor.getString(columnIndex);
        cursor.close();
        Bitmap bmp= BitmapFactory.decodeFile(filePath);

You need the actual path in the filesystem of the cropped image. UPDATE com.android.camera.action.CROP is not official android intent. Apps like gallery and camera support it but there are many devices where this intent is not supported. Have a look at this blog post by @commonsware. I have answered cropping image using a library in this SO post.

Community
  • 1
  • 1
Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
0

i don't know you might be aware of this Library its my suggestion only i am not giving answer in terms of code , i am giving option

why reinvent the wheel :) (but of course we can improve)

https://github.com/edmodo/cropper

MilapTank
  • 9,988
  • 7
  • 38
  • 53