Try this it didn't work for me but you can try Get filename and path from URI from mediastore as it has apparently worked for many people's. If you still get null you can do what I have done.
I tried many ways but I was still getting the result null. basically, I am trying to upload an image from local storage compress it, and used the compressed image.
val path = this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS)`try {
val file = File(path, "/Image")
file.mkdirs()
val image = File(file.absolutePath, "image1.jpeg")
intent1.data = image.absolutePath.toUri()
val bitmap = BitmapFactory.decodeStream(this.contentResolver.openInputStream(uri))
val array = ByteArrayOutputStream()
val out = FileOutputStream(image)
bitmap.compress(Bitmap.CompressFormat.JPEG, 70,out)
out.flush()
out.close()
Log.d("Path", image.absolutePath)
} catch (e: Exception) {
e.printStackTrace()
}`