2

I'm trying to pick video from gallery and want to get it's real path. But in the code it's showing Mediastore.Video.Media.DATA id deprecated in API Level 29. So please help me to find out the solution.

Here is my Video Picker Code:

    val i = Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(i, videoCode)

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == videoCode && resultCode == RESULT_OK) {
        videoPath = getPathFromUri(data!!.data)
    }
}

    private fun getPathFromUri(uri: Uri?): String? {
    var path:String?=null
    val projection = arrayOf(MediaStore.Video.Media.DATA)
    val cursor = contentResolver.query(uri!!, projection, null, null, null)
    if (cursor != null) {
        val columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA)
        cursor.moveToFirst()
        path=cursor.getString(columnIndex)
    }
    cursor?.close()
    return path
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Geek Tanmoy
  • 745
  • 1
  • 9
  • 20
  • 1
    The .DATA column is not available on Android 10 devices. But do not try to convert that nice uri to a file system path. Use that uri directly. – blackapps Sep 27 '20 at 11:19
  • 1
    "want to get it's real path" -- there is no "real path", at least not one that you can use reliably. Use the `Uri` that you are given, such as by passing it to `openInputStream()` on a `ContentResolver`. – CommonsWare Sep 27 '20 at 11:51
  • Thanks for the response. It will be useful if you share some code segment as example. – Geek Tanmoy Sep 29 '20 at 04:28

0 Answers0