简体   繁体   English

如何从意图数据uri获取文件路径

[英]how to get file path from intent data uri

I'm using intent to select a video,我正在使用意图 select 的视频,

fun openVideo(view: View) {
        val intent = Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI)
        intent.type = "video/*"
        startActivityForResult(
            Intent.createChooser(intent, "Select Video"),
            REQUEST_TAKE_GALLERY_VIDEO
        )
}

then i'm getting uri and path然后我得到uri和path

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_TAKE_GALLERY_VIDEO && data != null) {

            val videPath = data.data?.path 
            val selectedVideoUri: Uri? = data!!.data.toString()
        }
    }

output output

videPath:
/external_files/KannadaGeeta/05CHAPTER2.mp3

selectedVideoUri :
content://com.mi.android.globalFileexplorer.myprovider/external_files/KannadaGeeta/05CHAPTER2.mp3

but I need path like below to check whether file exist or not但我需要像下面这样的路径来检查文件是否存在

/storage/emulated/0/KannadaGeeta/13CHAPTER12.mp3

Look, by working on strings I can achieve what I want.看,通过处理字符串,我可以实现我想要的。 But I'm looking for a proper way to get the video path.但我正在寻找一种获取视频路径的正确方法。

Can anyone help me on this.谁可以帮我这个事。

Edit:编辑:

i tried this which is giving false我试过这个是假的

File(selectedVideoUri).exists()

You can simply replace the /external_files with the external directory.您可以简单地将/external_files替换为外部目录。

 val intentPath = activity?.intent!!.data!!.path!!

 val absolutePath = Environment.getExternalStorageDirectory().toString() + intentPath.substringAfter("/external_files") 

 e.g

 println("Intent Path : ${intentPath}")
 // Intent Path : external_files/KannadaGeeta/05CHAPTER2.mp3

 println("Absolute Path : ${absolutePath}")
 // Absolute Path : /storage/emulated/0/KannadaGeeta/05CHAPTER2.mp3

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

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