Home > Software design >  how to preview video and upload that video file to firebase in android kotlin
how to preview video and upload that video file to firebase in android kotlin

Time:06-30

I am trying to upload a video file to firebase.:-

this is the code:-

lateinit var file: Any


private fun selectingVideo() {
    val videoPickIntent = Intent(Intent.ACTION_PICK)
    videoPickIntent.type = "video/*"
    startActivityForResult(Intent.createChooser(videoPickIntent, "Please pick a video"), videoRequest   )
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == videoRequest && resultCode == Activity.RESULT_OK && null != data) {
            feedImageSelect.visibility = View.GONE
            feedVideoSelect.visibility = View.VISIBLE
            val pickedVideoUrl = getRealPathFromUri(applicationContext, data.data!!)
            file = pickedVideoUrl
            feedVideoSelect.setVideoPath(pickedVideoUrl)
            // Default Media-Controller
            feedVideoSelect.setMediaController(MediaController(this))
            // start playing
            feedVideoSelect.start()
        
    }
}

// Retrieve Video Path from URI.
private fun getRealPathFromUri(context: Context, contentUri: Uri): String {
    var cursor: Cursor? = null
    try {
        val proj = arrayOf(MediaStore.Images.Media.DATA)
        cursor = context.contentResolver.query(contentUri, proj, null, null, null)
        val columnIndex = cursor!!.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
        cursor.moveToFirst()
        return cursor.getString(columnIndex)
    } finally {
        cursor?.close()
    }
}

I am getting this as the error.:-

java.lang.ClassCastException: java.lang.String cannot be cast to android.net.Uri

I have tried all the possible ways to solve it. But Still I am getting this error. I tried other methods as well still they are not working for my requirements.

CodePudding user response:

Make these changes to the code:-

     val pickedVideoUrl = data.data

feedVideoSelect.setVideoPath(pickedVideoUrl.toString())
  • Related