简体   繁体   中英

how to get file path from intent data uri

I'm using intent to select a video,

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

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

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.

 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

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