0

I want to crop selected image from gallery to square image. I have button select_photo and image_view that displays photo. When user click on button it opens user gallery. I have tried several options that I have found on the web.

This is my code that create Intent and open gallery.

private fun choosePhotoFromGallary() {
    val galleryIntent = Intent(Intent.ACTION_PICK)
    galleryIntent.apply {
        type = "image/*"
        putExtra("crop", "true") 
        putExtra("outputX", 250)
        putExtra("outputY", 250)
        putExtra("scale", true)
        putExtra("aspectX", 1)
        putExtra("aspectY", 1)
        putExtra("return-data", true)
    }

    startActivityForResult(galleryIntent, GALLERY_REQUEST_CODE)
}

I use onActivityResult() to get my data back from the Intent.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (requestCode == GALLERY_REQUEST_CODE) {
        if(resultCode == Activity.RESULT_OK) {
            photoImageView.setImageURI(data?.data)

        }
    }
}

But i have no "cropping menu" available. So chosen photo is unchanged. I want it to be square so i need cropping. How can i launch "cropping menu" so user can crop the image?

ShadeToD
  • 403
  • 9
  • 22
  • 2
    Asked multiple times ... answer is: there is no "crop intent" - use 3rd party library – Selvin Aug 26 '19 at 10:54
  • I am new to Android development. I saw few answers that it is possible to do this without 3rd party library. If you can't do this "natively" then what is the library you would recommend? – ShadeToD Aug 26 '19 at 10:58
  • *what is the library you would recommend?* [section don't ask about](https://stackoverflow.com/tour) – Selvin Aug 26 '19 at 11:02
  • 2
    "I saw few answers that it is possible to do this without 3rd party library" -- they are mistaken. There are 2.5 billion Android devices, and while a few might support these undocumented/unsupported `Intent` extras, not all will. There is a whole category of image cropping libraries in [the Android Arsenal](https://android-arsenal.com/tag/45). – CommonsWare Aug 26 '19 at 11:04
  • Ok thanks for the clear answer. Now i understand using 3rd party library is the best option, because every device will have it installed when using my app. – ShadeToD Aug 26 '19 at 11:06
  • Check out this question https://stackoverflow.com/questions/15228812/crop-image-in-android – Bali Aug 26 '19 at 14:29

0 Answers0