简体   繁体   English

如何在android 11中从Uri获取真实路径

[英]How to get Real path from Uri in android 11

How to get Real path from URI in android 10 and android 11. Getting the Uri from intent data ie如何从 android 10 和 android 11 中的 URI 获取真实路径。 从意图数据中获取 Uri,即

 private val startForResult =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
            if (result.resultCode == Activity.RESULT_OK) {
                val uri = result.data?.data
              
            }
        }

and intent to get pdf file并打算获取pdf文件

 val mimeType = "application/pdf"
            val intent = Intent(Intent.ACTION_GET_CONTENT)
            intent.type = mimeType
            intent.addCategory(Intent.CATEGORY_OPENABLE)
 if (packageManager.resolveActivity(sIntent, 0) != null) {
            // it is device with Samsung file manager
            chooserIntent = Intent.createChooser(sIntent, "Select an PDF File")
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, arrayOf(intent))
        } else {
            chooserIntent = Intent.createChooser(intent, "Select an PDF File")
        }
        try {
            chooserIntent.resolveActivity(packageManager)?.let {
                //  startActivityForResult(Intent.createChooser(this,"Pick MHT file"),1)
                startForResult.launch(chooserIntent)
            }
        } catch (ex: ActivityNotFoundException) {
            Toast.makeText(
                applicationContext,
                "No suitable File Manager was found.",
                Toast.LENGTH_SHORT
            ).show()
        }

You can do this to get the data:您可以这样做来获取数据:

val inputStream = context.contentResolver.openInputStream(uri)

but I am not sure you can get a usable file path from the uri.但我不确定您是否可以从 uri 获得可用的文件路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM