Home » Android » java – Android image cropping is reducing the size of the image

java – Android image cropping is reducing the size of the image

Posted by: admin June 15, 2020 Leave a comment

Questions:

I need to crop a photo from the android phone gallery and then edit it.
The original size of the photo is 3264 *2448 which is displayed in my screen of size 1280 * 720 as a scaled down image.
When I crop it, i am getting a image of size 185 * 139.

The code I am using for cropping is

        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        cropIntent.setDataAndType(picUri, "image/*");
        cropIntent.putExtra("crop", "true");
        cropIntent.putExtra("return-data", true);
        startActivityForResult(cropIntent, PIC_CROP);

When I display the cropped image in an imageView it is displayed as a much smaller image.

I should be cropping the original sized image and not the scaled down version. Could any of you please guide me as to how to do this?

How to&Answers:

Please try this, i know its too late, but may help someone.

private void performCrop() {
try {
    //call the standard crop action intent (the user device may not support it)
    Intent cropIntent = new Intent("com.android.camera.action.CROP");
    //indicate image type and Uri
    cropIntent.setDataAndType(mImageCaptureUri, "image/*");
    //set crop properties
    cropIntent.putExtra("crop", "true");
    //indicate aspect of desired crop
    cropIntent.putExtra("aspectX", 4);
    cropIntent.putExtra("aspectY", 3);
    //indicate output X and Y
    cropIntent.putExtra("outputX", 800);
    cropIntent.putExtra("outputY", 800);

File f = new File(Environment.getExternalStorageDirectory(),
        "/temporary_holder.jpg");
    try {
        f.createNewFile();
    } catch (IOException ex) {
    Log.e("io", ex.getMessage());  
    }

uri = Uri.fromFile(f);

  cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);

    startActivityForResult(cropIntent, PIC_CROP);

} //respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
    //display an error message
    String errorMessage = "Your device doesn't support the crop action!";
    Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
    toast.show();
}
}

Make your onActivityResult like this :-

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

     if (requestCode == PIC_CROP) {

              String filePath = Environment.getExternalStorageDirectory()
                    + "/temporary_holder.jpg";

            thumbnail = BitmapFactory.decodeFile(filePath);



                   //thumbnail =    BitmapFactory.decodeFile(filePath);
           //    Log.i("",String.valueOf(thumbnail.getHeight()));

                ImageView image = (ImageView) findViewById(R.id.pestImage);
                image.setImageBitmap(thumbnail);
                }
}

Answer:

Add:

intent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);

Then you can get an image file which is not reduced. And use this saved file instead of:

Intent.getExtras().getParcelable("data")

Just ignore returned bitmap.

Answer:

First off you should be aware that the crop intent cannot be relied upon for cross-device compatibility. (Any OEM can replace the stock camera/gallery app with their own version). This aside, I think you are missing a few extras that should go in the intent and give you your desired effect.

// width & height are your desired width/height for the cropped result
cropIntent.putExtra("outputX", width);
cropIntent.putExtra("outputY", height);
cropIntent.putExtra("aspectX", width);
cropIntent.putExtra("aspectY", height);
cropIntent.putExtra("scaleUpIfNeeded", true);

Try adding those to your intent and see how it turns out.