Help with image cropping

scubby3scubby3 Member ✭✭
edited July 2018 in Xamarin.Android

Have this activity that allow me to take or select a phone and then crop it and set it to ImageView.
However after the image is crop it return null on Bitmap bitmap = (Bitmap)bundle.GetParcelable("data");
have also tried Bitmap bitmap = (Bitmap)data.extras.Get("data");
here is my code

    private void CameraOpen()
    {
        CamIntent = new Intent(MediaStore.ActionImageCapture);
        file = new File(Android.OS.Environment.ExternalStorageDirectory, "file_" + Guid.NewGuid().ToString() + ".jpg");
        uri = Android.Net.Uri.FromFile(file);
        CamIntent.PutExtra(MediaStore.ExtraOutput, uri);
        CamIntent.PutExtra("return-data", true);
        StartActivityForResult(CamIntent, 0);
    }

    private void GaleryOpen()
    {
        GalIntent = new Intent(Intent.ActionPick, MediaStore.Images.Media.ExternalContentUri);
        StartActivityForResult(Intent.CreateChooser(GalIntent, "Select image from galery"), 2);
    }

    protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
    {
        if (requestCode == 0 && resultCode == Result.Ok)
        {

            CropImage();
        }
        else if (requestCode == 2)
        {
            if (data != null)
            {
                uri = data.Data;
                CropImage();
            }
        }
        else if (requestCode == 1)
        {
            if (data != null)
            {
                Bundle bundle = data.Extras;
                Bitmap bitmap = (Bitmap)bundle.GetParcelable("data");
                imgPhoto.SetImageBitmap(bitmap);
            }

        }
    }

    private void CropImage()
    {
        try
        {
            CropIntent = new Intent("com.android.camera.action.CROP");
            CropIntent.SetDataAndType(uri, "image/*");

            CropIntent.PutExtra("crop", "true");
            CropIntent.PutExtra("outputX", 180);
            CropIntent.PutExtra("outputY", 180);
            CropIntent.PutExtra("aspectX", 4);
            CropIntent.PutExtra("aspectY", 4);
            CropIntent.PutExtra("scaleUpIfNeeded", true);
            CropIntent.PutExtra("return-data", true);

            StartActivityForResult(CropIntent, 1);
        }
        catch (ActivityNotFoundException ex)
        {

        }
    }

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
    {
        switch (requestCode)
        {
            case RequestPermissionCode:
                {
                    if (grantResults.Length > 0 && grantResults[0] == Permission.Granted)
                        Toast.MakeText(this, "Permission Granted", ToastLength.Short).Show();
                    else
                        Toast.MakeText(this, "Permission Canceled", ToastLength.Short).Show();
                }
                break;
        }
    }

Error am getting

Unhandled Exception:

System.NullReferenceException:

greatly appreciate the help.

Best Answer

Answers

Sign In or Register to comment.