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
}
1

There are 1 answers

0
Manisha Saini On
 private fun pickImage() {
    val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
    intent.addCategory(Intent.CATEGORY_OPENABLE)
    intent.type = "image/*"
    imageActivityResultLauncher.launch(intent)
}
  private fun pickVideo() {
    val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
    intent.addCategory(Intent.CATEGORY_OPENABLE)
    intent.type = "video/*"
    videoActivityResultLauncher.launch(intent)
}
 private var imageUri: Uri? = null
private var imgMultiPart: MultipartBody.Part? = null
private var selectedVideoUri: Uri? = null
private var videoMultiPart: MultipartBody.Part? = null

private val imageActivityResultLauncher =
    registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
        if (result.resultCode == Activity.RESULT_OK) {
            result.data?.let { data ->
                imageUri = data.data!!
                imgMultiPart = uriToMultipartBodyPart(this, imageUri!!)
                chatAdapter.insertData(
                    ChatMessage(
                        "",
                        sharedPref.getCurrentUser()?.id,
                        1,
                        "",
                        imageUri.toString(),
                        null,
                        getCurrentTime()
                    )
                )
                binding.rv.scrollToPosition(chatAdapter.itemCount - 1)
                val map = HashMap<String, RequestBody>()
                map["receiver_id"] = send_to_id.toRequestBody()
                map["message_type"] = "1".toRequestBody()
                viewModel.sendMedia(map, imgMultiPart!!)
            }
        }
    }


 private val videoActivityResultLauncher =
    registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
        if (result.resultCode == Activity.RESULT_OK) {
            result.data?.let { data ->
                selectedVideoUri = data.data
                selectedVideoUri?.let { videoUri ->
                    videoMultiPart = videoUriToMultipartBodyPart(this, videoUri)

                    chatAdapter.insertData(
                        ChatMessage(
                            "",
                            sharedPref.getCurrentUser()?.id,
                            1,
                            "",
                            "https://i.pinimg.com/736x/79/14/e5/7914e5821c09251cca436ad41e18e8d8.jpg",
                            null,
                            getCurrentTime()
                        )
                    )
                    binding.rv.scrollToPosition(chatAdapter.itemCount - 1)
                    val map = HashMap<String, RequestBody>()
                    map["receiver_id"] = send_to_id.toRequestBody()
                    map["message_type"] = "1".toRequestBody()
                    viewModel.sendMedia(map, videoMultiPart!!)
                }
            }
        }
    }

 private fun uriToMultipartBodyPart(
    context: Context,
    selectedImageUri: Uri
): MultipartBody.Part? {
    try {
        val contentResolver: ContentResolver = context.contentResolver
        val inputStream: InputStream? =
            if (ContentResolver.SCHEME_CONTENT == selectedImageUri.scheme) {
                contentResolver.openInputStream(selectedImageUri)
            } else {
                FileInputStream(selectedImageUri.path)
            }
        if (inputStream != null) {
            val imageRequestBody =
                inputStream.use { it.readBytes().toRequestBody("image/*".toMediaTypeOrNull()) }
            return MultipartBody.Part.createFormData(
                "media[]",
                selectedImageUri.lastPathSegment,
                imageRequestBody
            )
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return null
}

  private fun videoUriToMultipartBodyPart(context: Context, videoUri: Uri): MultipartBody.Part? {
    try {
        val contentResolver: ContentResolver = context.contentResolver
        val inputStream: InputStream? = try {
            when {
                ContentResolver.SCHEME_CONTENT == videoUri.scheme -> {
                    contentResolver.openInputStream(videoUri)
                }

                ContentResolver.SCHEME_FILE == videoUri.scheme -> {
                    FileInputStream(File(videoUri.path))
                }

                else -> null
            }
        } catch (e: IOException) {
            e.printStackTrace()
            null
        }
        if (inputStream != null) {
            val requestBody =
                inputStream.use { it.readBytes().toRequestBody("video/*".toMediaTypeOrNull()) }
            return MultipartBody.Part.createFormData(
                "media[]",
                videoUri.lastPathSegment,
                requestBody
            )
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return null
}