How to get real file path from URI while using storage access framework in android Q and android R. My code to pick pdf file using system file picker i.e.
Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
type = "application/pdf"
addCategory(Intent.CATEGORY_OPENABLE)
flags = flags or Intent.FLAG_GRANT_READ_URI_PERMISSION
startActivityForResult(intent, OPEN_PDF_REQUEST)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == OPEN_PDF_REQUEST && resultCode == RESULT_OK) {
data?.data?.also { documentUri ->
//Permission needed if you want to retain access even after reboot
contentResolver.takePersistableUriPermission(
documentUri,
Intent.FLAG_GRANT_READ_URI_PERMISSION
)
val documentFile = DocumentFile.fromSingleUri(this, documentUri)
val fileUri = documentFile?.uri
Log.d("TAG", "path = ${fileUri?.path}")
}
}
}
Logcat OUTPUT
URI= content://com.android.providers.media.documents/document/document%3A266
I am unable to get real file path from this URI in order to open the file within PdfViewer and also unable to open this URI in PDFViewer.
Now, how to get real file path from this URI or any alternative solution in order to achieve this goal (i.e. open pdf file in pdf viewer) in android Q and above.
Any help will be appreciated :)