如何从pdf URI android获取pdf路径?

时间:2019-12-09 07:52:09

标签: android

 val intent = Intent(Intent.ACTION_GET_CONTENT)
 intent.type = "application/pdf"
 try {
     startActivityForResult(intent, CommonConstant.REQUEST_CODE)
 } catch (e: Exception) {
     Toast.makeText(this, "Activity not found", Toast.LENGTH_SHORT).show()
 }

OnActivityResult()方法:

     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
            super.onActivityResult(requestCode, resultCode, data)
            if (resultCode == Activity.RESULT_OK && requestCode == CommonConstant.REQUEST_CODE) {
                val uri = data!!.data
                val path = uri?.toString()
                Log.e("PDF Path : ", path)
    }

}

从uri获取路径时,它只会给我uri字符串。

1 个答案:

答案 0 :(得分:0)

使用:

val path = uri?.path // "/mnt/sdcard/FileName.mp3"

val path = uri?.toString() // "file:///mnt/sdcard/FileName.mp3"