Q Android 10/11(R) 分区存储适配( 三 )


// Request code for creating a PDF document.const val CREATE_FILE = 1private fun createFile(pickerInitialUri: Uri) {val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {addCategory(Intent.CATEGORY_OPENABLE)type = "application/pdf"putExtra(Intent.EXTRA_TITLE, "invoice.pdf")// Optionally, specify a URI for the directory that should be opened in// the system file picker before your app creates the document.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri)}startActivityForResult(intent, CREATE_FILE)}2)打开文档
建议使用 type 设置 MIME 类型
// Request code for selecting a PDF document.const val PICK_PDF_FILE = 2fun openFile(pickerInitialUri: uri) {val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {addCategory(Intent.CATEGORY_OPENABLE)type = "application/pdf"// Optionally, specify a URI for the file that should appear in the// system file picker when it loads.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri)}startActivityForResult(intent, PICK_PDF_FILE)}3)授予对目录内容的访问权限
用户选择目录后 , 可访问该目录下的所有内容


推荐阅读