3

Initially I choose a Intent chooser to pick up a media file, and in onActivityResult i got the uri

fun openVideo(view: View) {
        val intent = Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI)
        intent.type = "video/*"
        startActivityForResult(
            Intent.createChooser(intent, "Select video"),
            REQUEST_TAKE_GALLERY_VIDEO
        )
    }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
       ...
       ...     
            val selectedVideoUri: Uri? = data!!.data
            val vidPath: String = selectedVideoUri.toString()
            video_uri = Uri.parse(vidPath)

// the video uri looks like below      
//content://com.mi.android.globalFileexplorer.myprovider/external_files/Download/Dolittle%20(2020)%20%5B720p%5D%20%5BBluRay%5D%20%5BYTS.MX%5D/Dolittle.2020.720p.BluRay.x264.AAC-%5BYTS.MX%5D.mp4
            
           initializePlayer(video_uri)
       ...
       ...
        }
    }

Then I was able to playing the local file in exoplayer.

 fun initializePlayer(video_uri) {
        println("log initializePlayer called")
        player = ExoPlayerFactory.newSimpleInstance(this) // `player` was defined in GlobalVariables
        id_player_view.player = player

        if (video_uri != null) {
            println("log $video_uri ${video_uri!!::class.simpleName}")
            id_player_view.useController = true
            var mediaSource: MediaSource = buildMediaSource(video_uri!!)
            player?.playWhenReady = playWhenReady!!
            player?.seekTo(currentWindow!!, playBackPosition!!)
            player?.prepare(mediaSource, false, false)
        }
    }

But now, think that I saved the uri in database as a string. Then, assume I retrieved the content from database and pass it as a intent to video playing activity

now, if i write a code like this, and call initializePlayer the video is not playing.

video_uri = Uri.parse((intent.getStringExtra("video_url")))
println("log $video_uri ${video_uri!!::class.simpleName}")

initializePlayer(video_uri)

i.e for all hardcoded content uri of local media files. the exoplayer is not working.

How to solve this issue ?

Vikas Acharya
  • 3,550
  • 4
  • 19
  • 52

1 Answers1

2

think that I saved the uri in database as a string

You no longer have rights to access the content.

How to solve this issue ?

Use ACTION_OPEN_DOCUMENT to let the user choose the content. Then, in onActivityResult(), call takePersistableUriPermission() on a ContentResolver, to have access to that content for a longer period of time. See this blog post and the documentation for more.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I went through the link its very good. but i cannot use `ACTION_OPEN_DOCUMENT` for audio and video. any suggestion please. – Vikas Acharya Jul 15 '20 at 12:41
  • 1
    @naanu: "but i cannot use ACTION_OPEN_DOCUMENT for audio and video" -- why not? `ACTION_OPEN_DOCUMENT` supports all sorts of MIME types. – CommonsWare Jul 15 '20 at 12:44
  • but when i did so, the ui of file picker completely changed. So i thought it is only for document. can't we do this with Action Pick itself ? – Vikas Acharya Jul 15 '20 at 12:49
  • 1
    @naanu: "the ui of file picker completely changed" -- you have no control over the UI for `ACTION_PICK`, `ACTION_OPEN_DOCUMENT`, or any other external activity. They will vary by Android OS version, device manufacturer, and occasionally based on what third-party apps are installed. "can't we do this with Action Pick itself ?" -- you are welcome to use `ACTION_PICK`, but do not save that `Uri` to a file or database. Use it only within your running process. If you need to be able to save the `Uri`, then you need to use `ACTION_OPEN_DOCUMENT`. – CommonsWare Jul 15 '20 at 12:52
  • Thanks, can you kindly tell me how to check for file exists or not from the uri obtained from `ACTION_OPEN_DOCUMENT`. the uri I got is this `content://com.android.providers.media.documents/document/audio%3A49393` because with `Action Pick` i was just taking last substring and with environment variable i'm checking file exits or not. but now the file name got encoded – Vikas Acharya Jul 15 '20 at 12:56
  • 1
    @naanu: `DocumentFile.fromSingleUri(uri).exists()`. – CommonsWare Jul 15 '20 at 12:58