0

I want to select a file from Gallery and then trying to crop it the problem i am facing is:

After cropping the image and on activity result i am getting error because i am receiving null,

Snippet of Activity On Result

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK)
            return;

        switch (requestCode) {
            case PICK_FROM_CAMERA:
                /**
                 * After taking a picture, do the crop
                 */
                doCrop();

                break;

            case PICK_FROM_FILE:
                /**
                 * After selecting image from files, save the selected path
                 */
                Toast.makeText(this,"in gall",Toast.LENGTH_LONG).show();
                mImageCaptureUri = data.getData();
               Log.e("URI", mImageCaptureUri.toString());
                doCrop();

                break;

            case crop:
                Log.e("URI2", mImageCaptureUri.toString());

                Bundle extras = data.getExtras();
                /**
                 * After cropping the image, get the bitmap of the cropped image and
                 * display it on imageview.
                 */

                    Bitmap photo = extras.getParcelable("data");
                    Log.e("URI9", mImageCaptureUri.toString());
                      //here i am receiving null idk why.....
                    mImageView.setImageBitmap(photo);


                //    File f = new File(mImageCaptureUri.getPath());
                /**
                 * Delete the temporary image
                 */
                //  if (f.exists())
                //    f.delete();

                break;

        }
    }

This is my Do Crop Function

    private void doCrop() {
        Intent intent = new Intent();
// call android default gallery
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
// ******** code for crop image
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 0);
        intent.putExtra("aspectY", 0);
        intent.putExtra("outputX", 200);
        intent.putExtra("outputY", 150);

        try {

            intent.putExtra("return-data", true);
            startActivityForResult(Intent.createChooser(intent,
                    "Complete action using"), crop);

        } catch (ActivityNotFoundException e) {
// Do nothing for now
        }

        }

}

i have declared crop as: private static final int crop = 5;

Kumail Hussain
  • 869
  • 2
  • 26
  • 49
  • [Android does not have a `CROP` `Intent`](https://commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html). There are many [image cropping libraries available for Android](https://android-arsenal.com/tag/45). Please use one. – CommonsWare Nov 01 '16 at 21:02
  • I think this question is a duplicate from http://stackoverflow.com/questions/27294252/cropping-image-in-android-crop-intent – Christine Nov 01 '16 at 21:03

1 Answers1

1

Ops i just neglected the null check everything is fine new updated code , error free code.

     @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK)
            return;

        switch (requestCode) {
            case PICK_FROM_CAMERA:
                /**
                 * After taking a picture, do the crop
                 */
                doCrop();

                break;

            case PICK_FROM_FILE:
                /**
                 * After selecting image from files, save the selected path
                 */
                Toast.makeText(this,"in gall",Toast.LENGTH_LONG).show();
                mImageCaptureUri = data.getData();
               Log.e("URI", mImageCaptureUri.toString());
                doCrop();

                break;

            case crop:
                Log.e("URI2", mImageCaptureUri.toString());

                Bundle extras = data.getExtras();
                /**
                 * After cropping the image, get the bitmap of the cropped image and
                 * display it on imageview.
                 */
                    if(extras!=null)
{
                    Bitmap photo = extras.getParcelable("data");
                    Log.e("URI9", mImageCaptureUri.toString());
                    mImageView.setImageBitmap(photo);
}

                //    File f = new File(mImageCaptureUri.getPath());
                /**
                 * Delete the temporary image
                 */
                //  if (f.exists())
                //    f.delete();

                break;

        }
    }
Kumail Hussain
  • 869
  • 2
  • 26
  • 49