Kotlin: Mediaplayer: Couldn't open content://com.android.externalstorage.: java.lang.SecurityException: Permission Denial: reading com.android.ext

Viewed 101

Not able to play a song stored as a Uri in SQLite, The file selected by below function is playing well

private fun selectMusicFile() {
    val intent = Intent()
    intent.type = "audio/*"
    intent.action = Intent.ACTION_GET_CONTENT
    startActivityForResult(Intent.createChooser(intent, "Select Audio"), 123)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == 123) {
        if (data?.data != null){
            musicFileUri = data?.data!!
            binding.tvMusicFile.text = data?.data!!.toString()
            setupMediaPlayer()
        }
    }
}

The same uri is stored in SQLite as a text field. When i try to play this uri then i get error here is code for setting up the mediaplayer using uri from SQLite which gets store in the textview

  private fun setupMediaPlayer() {
    val vUri = Uri.parse(binding.tvMusicFile.text.toString())
    mediaPlayer.setDataSource(this, vUri)
    mediaPlayer.prepare()

}

it fails at setDataSource

setupMediaPlayer() works well when called from onActivityResult, but fails when called from other place e.i. after fetching uri from SQLite table

Can someone please help

here is error log

W/MediaPlayer: Couldn't open content://com.android.externalstorage.documents/document/primary%3AMusic%2FThe%20Man%20Machine%2FThe%20Model.mp3: java.lang.SecurityException: Permission Denial: reading com.android.externalstorage.ExternalStorageProvider uri content://com.android.externalstorage.documents/document/primary%3AMusic%2FThe%20Man%20Machine%2FThe%20Model.mp3 from pid=23509, uid=10264 requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs E/MediaPlayerNative: Unable to create media player D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main Process: com.xxx, PID: 23509 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx/com.xxx.configure.ConfigureActivity}: java.io.IOException: setDataSource failed.: status=0x80000000 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2861) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2943) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1630) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6626) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811) Caused by: java.io.IOException: setDataSource failed.: status=0x80000000 at android.media.MediaPlayer.nativeSetDataSource(Native Method) at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1172) at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1160) at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1078) at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1003) at com.xxx.configure.ConfigureActivity.setupMediaPlayer(ConfigureActivity.kt:189) at com.xxx.configure.ConfigureActivity.onCreate(ConfigureActivity.kt:115) at android.app.Activity.performCreate(Activity.java:7032) at android.app.Activity.performCreate(Activity.java:7023) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1236) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2814) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2943)  at android.app.ActivityThread.-wrap11(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1630)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6626)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811) 

0 Answers
Related