0

Hello I have to develope a program meeting the next requirements I have to use Kotlin I have to use Ucrop Yalantis Library I have to use contracts I have to crop images from picker and camera

I've been able to design the picker a crop the image but I have no idea about how to use Ucrop with the camera using contracts. Any suggestion?

This is my code for the picker It works. Besides I'm able to capture the image from the camera, so I have its Uri but I don`t know how to crop the image I get frome the camer with Ucrop. If you need I can add the code for the camera but the post should be long.

file:///data/user/0/com.example.tfg_red_social_v0/files/croppedImage.jpg

The file exists I've checked with the explorer

private val cropImage = registerForActivityResult(uCropContract){uri->
     Glide.with(this).load(uri).circleCrop().into(binding.ivPerfil)
}

Please any help is welcome I'm adding extra code

My manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <!-- PERMISOS PARA ACCEDER A INTERNET, CAMARA Y SD -->
    <uses-permission android:name="android.permission.INTERNET" />
   <!-- <uses-permission android:name="android.permission.CAMERA" /> -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" tools:ignore="ScopedStorage"/>
    <uses-feature android:name="android.hardware.camera" android:required="true" />
this is my provider

<provider
            android:authorities="com.example.tfg_red_social_v0.fileprovider"
            android:name="androidx.core.content.FileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

and my file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-files-path
        name="my_debug_images"
        path="Pictures"/>
</paths>

And here how I use UCROP

    private val uCropContract = object: ActivityResultContract<List<Uri>, Uri>(){
        override fun createIntent(context: Context, input: List<Uri>): Intent {
            val inputUri =  input[0]
            val outPut =  input[1]
     
            val uCrop = UCrop.of(inputUri,outPut)
                .withAspectRatio(16f,9f)
            return uCrop.getIntent(context)

        }

        override fun parseResult(resultCode: Int, intent: Intent?): Uri {
            return UCrop.getOutput(intent!!)!!
        }
    }

    private val cropImage = registerForActivityResult(uCropContract){uri->
        imagenUri=uri
        Glide.with(this).load(uri).into(binding.ivImagen)
      }

    private val getContent = registerForActivityResult(ActivityResultContracts.GetContent()){uri->
        val inputUri = uri
        val outputUri = File(requireContext().filesDir, "croppedImage.jpg").toUri()
        val listUri = listOf<Uri>(inputUri!!,outputUri)
        cropImage.launch(listUri)
    }
}

And I start crop with

  binding.ivImagen.setOnClickListener{
        getContent.launch("image/*")
    }
Monrac
  • 49
  • 1
  • 6

1 Answers1

0

Sorry for bothering you I fixed it. It was a piece of cake

the only thing you have to do is replace

 private val getContent = registerForActivityResult(ActivityResultContracts.GetContent()){uri->
        val inputUri = uri
        val outputUri = File(requireContext().filesDir, "croppedImage.jpg").toUri()
        val listUri = listOf<Uri>(inputUri!!,outputUri)
        cropImage.launch(listUri)

with

private val recuperaImagenCamara = registerForActivityResult(ActivityResultContracts.TakePicture()){
        success ->
    if (success) {
        val inputUri = imagenUri
        val outputUri = File(requireContext().filesDir, "croppedImage.jpg").toUri()
        val listUri = listOf<Uri>(inputUri!!,outputUri)
        cropImage.launch(listUri)
        Glide.with(this@PublicacionFragment).load(imagenUri).circleCrop().into(binding.ivImagen)
        binding.ivImagen.setImageURI(imagenUri)
    } else
        Log.e(ContentValues.TAG,"la URI no se guardo es: $imagenUri")
}

I hope it helps to anyone

Monrac
  • 49
  • 1
  • 6
  • can you please explain how you crop image from camera capture using this UCrop...? im having difficulty understanding your own fix for example like where is "imagenUri" came from in "val inputUri = imagenUri"... etc etc – Dioo B Feb 03 '23 at 01:36
  • Hi. It's a bit hard to explain in such a small text. imagenUri is the URI of the picture I get from registerForActivityResult and outputUri is the URI of the new file to content the cropped image. So listUri is pair made of the imagen without cropping and the cropped imagen. I'm not sure If've been able to enlight somenting or it worse now – Monrac Feb 04 '23 at 19:01