Why File(uri.path) work but not uri.toFile() (error: Uri lacks 'file' scheme)

Viewed 635

All is in the title.

I'm able to get a File object by using the File constructor but using the toFile() extension on the Uri led to an error.

java.lang.IllegalArgumentException: Uri lacks 'file' scheme: content://***.provider/files/06082021_113127.jpg

What I do :

private var uri: Uri? = null

binding.camera.setOnClickListener {

    val name = LocalDateTime.now().generateFileName()
    val file = File(requireContext().filesDir, "$name.jpg")
    uri = FileProvider.getUriForFile(
        requireContext(),
        requireContext().packageName + ".provider",
        file
    )

    takePicture.launch(uri)

}

private val takePicture = registerForActivityResult(ActivityResultContracts.TakePicture()) { success: Boolean ->

        val test = uri?.path?.let { File(it) }
        val test2 = uri?.toFile()

        Log.e("test", "${test?.name}")
        Log.e("test2", "${test2?.name}")

    }

Can someone help shed some light ?

0 Answers
Related