Solution 1 :
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
Problem :
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()
Comments
Comment posted by blackapps
You do not have to check if the file exists. If the file did not exist the user could not have picked it.
Comment posted by blackapps
You should save
Comment posted by blackapps
You realise that
Comment posted by blackapps
I do not understand what you mean with ‘i do not want to work on strings’. A path is a string too isnt it?
Comment posted by blackapps
You do not have to format anything. And you should not try to convert an uri -a nice content scheme- to a file system path. As a professional you should use the obtained uri directly. What is the reason you want a path? That uri is all you need.
Comment posted by Vikas Acharya
I have already mentioned in my question “Look, by working on strings I can achieve what I want. But I’m looking for a proper way to get the video path.”