Android Scoped Stoage - get video details in onActivityResult

170 Views Asked by At

Use case - Currently I'm working on video uploading module. To get the video i have fired SAF video picker intent but I can't get any way or code to get the video details from uri in OnActivityResult even i have watched Google IO 19 Preparing Scoped Storage Video but there also not provided any sample code how to do that. ( Video details mean duration size name resolution and name )

To get the video details we have to use MediaStore api which recommended one and MediaStore only works with content URIs if i pass file uri then cursor will be always null and we only get the content uri when cursor isnot null

Hoping to someone will help me

I'm opening video chooser using this code

    private fun openVideoChooser() {
    val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
    intent.type = "video/*"
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false)
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), 100)
}

in onActivityResult have called this function by passing uri got from intent-

    private fun getVideoDetails(it: Uri) {
    var fileName: String? = null
    var duration: Long = 0
    val projection = arrayOf(MediaStore.Video.VideoColumns.DATA, MediaStore.Video.VideoColumns.DURATION)
    val cursor = contentResolver.query(it, projection, null, null, null)
    if (cursor != null) {
        if (cursor.moveToFirst()) {
            fileName = cursor.getString(cursor.getColumnIndexOrThrow(projection[0]))
            duration = cursor.getLong(cursor.getColumnIndex(projection[1]))
        }
        cursor.close()
    } else
        LogUtil.info("videoInfo ", "Cursor Null :")
    if (fileName == null) fileName = "NA"
    LogUtil.info("videoInfo ", "Path : $fileName")
    LogUtil.info("videoInfo ", "Duration : $duration")
}

have searched for solution over internet but not got any solution so posting here. My testing device is Honor 8x ( Android 10 Version )

0

There are 0 best solutions below