简体   繁体   中英

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

 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

 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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