I'm trying to get a byte of a document inside of sdcard. What I do is exactly the following;
1-) I choose the file.
fun openFile() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "application/pdf"
}
startActivityForResult(intent, PICK_PDF_FILE)
}
2-) I now have a URI. I'm trying to convert it to a file. Then I try to get the bytes.
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData)
if (requestCode == PICK_PDF_FILE && resultCode == Activity.RESULT_OK) {
resultData?.data?.also { documentUri ->
contentResolver.takePersistableUriPermission(
documentUri,
Intent.FLAG_GRANT_READ_URI_PERMISSION
)
var file = documentUri.toFile(); // I am trying to convert URI to FILE type. //ERROR LINE
Log.d("test" , file.readBytes().toString()) // I'm trying to read the bytes.
}
}
}
But this error:
Caused by: java.lang.IllegalArgumentException: Uri lacks 'file' scheme:
content://com.android.externalstorage.documents/document/primary%3ADCIM%2Ftest.pdf