1

I am trying to make a File object from Uri which I get by selecting file from file explorer. Then I need to send this File to server. I get Uri object in following way:

private val registerActivityForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
    it.data?.data?.let { imageUri ->
        // handle Uri
    }
}

private val openFileExplorer: () -> Unit = {
    val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
        addCategory(Intent.CATEGORY_OPENABLE)
        type = "*/*"
    }
    registerActivityForResult.launch(intent)
}

When I try to parse Uri to File by calling Uri.toFile() method, it throws an exception:

java.lang.IllegalArgumentException: Uri lacks 'file' scheme: content://com.android.providers.media.documents/document/image%3A1000000471

What is the correct way to get a File object by selecting a file from file explorer?

AndroideuszPL
  • 385
  • 1
  • 2
  • 13

0 Answers0