my code does not enter onActivityResult (Kotlin)

Asked

Viewed 104 times

-1

Good afternoon, I’m trying to take a photo with the camera phone and save it in a database, however I can’t recover the image bitmap, I can open the camera and take the photo but when entering onActivityResult the app does not enter the class, here’s what I’m trying to do:

private fun abrirCamera() {

    Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
        // Ensure that there's a camera activity to handle the intent
        takePictureIntent.resolveActivity(packageManager)?.also {
            // Create the File where the photo should go
            val photoFile: File? = try {
                createImageFile()
            } catch (ex: IOException) {
                // Error occurred while creating the File
                null
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                photoFile?.also {
                    val photoURI: Uri = FileProvider.getUriForFile(
                        this,
                        "com.example.xamodeliveryloja.fileprovider",
                        it
                    )
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
                    startActivityForResult(takePictureIntent, PICK_IMAGE_CODE)
                }
            }
        }
    }

}

@Throws(IOException::class)
private fun createImageFile(): File {
    // Create an image file name
    val timeStamp: String = SimpleDateFormat("ddMMyyy_HHmmss").format(Date())
    val storageDir: File? = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
    return File.createTempFile(
        "JPEG_${timeStamp}_", /* prefix */
        ".jpg", /* suffix */
        storageDir /* directory */
    ).apply {
        // Save a file: path for use with ACTION_VIEW intents
        currentPhotoPath = absolutePath
    }
}



 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
            try {
                when(requestCode){
                    PICK_IMAGE_CODE ->
                        if(resultCode == Activity.RESULT_OK && data != null){
                            //val thumbnail = MediaStore.Images.Media.getBitmap(
                            //contentResolver, imageUri)
                             //  bitFotoSelecionada = data.extras?.get("data") as Bitmap
                            // colocaFotoProduto.setImageBitmap(bitFotoSelecionada)
        
                            val file = File(currentPhotoPath)
                            val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, Uri.fromFile(file))
                            if(bitmap != null){
                                colocaFotoProduto.setImageBitmap(resize(this,bitmap,100F,100F))
                                bitFotoSelecionada = bitmap
                            }
                        }
        
                    PICK_IMAGE ->
                        if(resultCode == Activity.RESULT_OK && data != null){
                            val uri = data.data
                            bitFotoSelecionada = MediaStore.Images.Media.getBitmap(this.contentResolver, uri)
                            colocaFotoProduto.setImageBitmap(resize(this,bitFotoSelecionada,100F,100F))
                        }
        
                }
            }catch (e: Exception){
                e.printStackTrace()
        
        }
    }

I’m using this method because I need the image quality to be good, I followed the documentation from google here, this code works on my emulator on the pc but already tested on 2 phones one with android 6 and one with android 8 however not one of the two enters onActivityResult, I can get the image using the miniature method

    private fun abrirCamera() {
    val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
    startActivityForResult(intent, PICK_IMAGE_CODE)

}

with this works normally but the quality gets very bad

1 answer

0

Good afternoon, I managed to solve the problem I apologize for the inconvenience was a mistake beast but I was not able to debug in my cell because he and very old managed to debug in another and I discovered the problem was in this IF

resultCode == Activity.RESULT_OK && data != null

Browser other questions tagged

You are not signed in. Login or sign up in order to post.