示例代码如下:
//启动权限弹框
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
}
}
//在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)
}
评论
0 条评论
请登录写评论。