The sample code is below:
//start the permission request
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val storageManager = requireActivity().getSystemService(Context.STORAGE_SERVICE) as StorageManager
val volume: StorageVolume? = storageManager.getStorageVolume(File(curMissionPath))
if (volume != null) {
val intent = volume.createOpenDocumentTreeIntent()
startActivityForResult(intent, OPEN_DOCUMENT_TREE)
return
}
}
//Add in onActivityResult
if (requestCode == OPEN_DOCUMENT_TREE) {
val uri = data!!.data
requireActivity().grantUriPermission(getPackageName(), uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION)
val takeFlags = data.flags and (Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION)
requireActivity().getContentResolver().takePersistableUriPermission(uri!!, takeFlags)
}
Comments
0 comments
Please sign in to leave a comment.