Ucrop yalantis contracts kotlin camera

269 Views Asked by At

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/*")
    }
1

There are 1 best solutions below

2
On

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