I am trying to play a video in exoplayer from a file located in storage.
At first I get the uri for specific file and store it in a string variable.
private var myURI: String? = null
private var getURI = registerForActivityResult(
ActivityResultContracts
.GetContent()
) { uri ->
if (uri == null) {
Toast.makeText(
requireActivity(), "Please pick a video",
Toast.LENGTH_SHORT
).show()
} else {
Log.d(ContentValues.TAG, "URI is $uri")
//Store URI
myURI = uri.toString()
}
}
And further, I store the URI to the local SQLite database on the device.
I already have it's URI in local database so I don't want to use registerForActivityResult method again and don't want to choose video by Intent. This is how uri looks in string format in database.
"content://com.android.providers.media.documents/document/video%3A51407"
This is the code to access uri from the database
val uri = URI("content://com.android.providers.media.documents/document/video%3A51407")
val videoUri = uri.toString()
I have tried to find a way to do so but can't find a way to implement this.
I store the URI to the local SQLite database on the device
That is not going to work as written. You need to switch from ActivityResultContracts.GetContent
to ActivityResultContracts.OpenDocument
. You also need to call takePersistableUriPermissions()
on a ContentResolver
once you get the Uri
. Otherwise, your rights to that content lapse once your process terminates.