I am getting open failed: EACCES (Permission denied) error in android Q only while using scope storage access framework. The app is working fine on android 11 but getting this error on android Q only. Here is my code:
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
type = "text/xml"
addCategory(Intent.CATEGORY_OPENABLE)
//Adding Read URI permission
flags = flags or Intent.FLAG_GRANT_READ_URI_PERMISSION
}
startActivityForResult(intent, OPEN_XML_FILE_REQUEST)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode == OPEN_XML_FILE_REQUEST && resultCode == RESULT_OK){
data?.data?.let { documentUri ->
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
//Permission needed if you want to retain access even after reboot
contentResolver.takePersistableUriPermission(
documentUri,
Intent.FLAG_GRANT_READ_URI_PERMISSION
)
//getting real path by using Handle Path Oz
handlePathOz.getRealPath(documentUri)
}
}
}
override fun onRequestHandlePathOz(pathOz: PathOz, tr: Throwable?) {
val path = pathOz.path
//gets the real path of file, now getting the file content and opening it in an editor
}
In order to get real path of file i have used Handle Path Oz because from documentUri i was unable to get real path of file. Here is library
Note: Please Don't suggest for to add android:requestLegacyExternalStorage="true" in AndroidManifest.xml file as google playstore does not allowed it now while targeting android SDK 30