skip to Main Content

TL;DR: I get the following error when I try to read a file (Uri) in a service I get the following error:

java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider

What I am trying to do:

a) read an audio file at specific time

b) have the option to stop the Media Player using the app interface (stopService())

c) make sure that even if the app is closed, the audio still gets played


How I am trying to do it:

  1. Select an audio file using ACTION_GET_CONTENT in Main Activity
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult())
     { result ->if (result.resultCode == RESULT_OK) {
                    val data: Intent? = result.data
                    fileUri = data?.data!!
                }
            }
    
    val myIintent = Intent()
                    .setType("audio/*")
                    .setAction(Intent.ACTION_GET_CONTENT)

    resultLauncher.launch(myIntent)
  1. Set an alarm using a pending intent AlarmManager, and pass the Uri (as a string) using extras to the AlarmManager

     val alarmIntent = Intent(context,AlarmSetter::class.java)
     alarmIntent.action = "SET_ALARM"
     alarmIntent.putExtra("audio_path",fileUri.toString())
    
     alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
     var pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0)
    
     alarmManager.setAndAllowWhileIdle(
         AlarmManager.RTC_WAKEUP,
         calendar.timeInMillis,
         pendingIntent)
    
  2. Call the service in the receiver and pass the Uri to the service in the intent
    override fun onReceive(context: Context?, intent: Intent?) {

     val filePath = intent?.getStringExtra("audio_path")
     val musicIntent = Intent(context, MusicService::class.java)
     musicIntent.putExtra("audio_path",filePath)
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
     context?.startForegroundService(musicIntent)}
     else{
         context?.startService(musicIntent)
     }
    
  3. Read the file in the service

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

       ...
       notification()
       ...

       val mp = MediaPlayer.create(this, Uri.parse(intent?.getStringExtra("audio_path")))

       ...

Problem:

When the App is still running, the service sends the notification is sent and the audio file is read by the media player.

However, when the app is killed/exited, the notification is sent but the file can’t be read because the service does not have permission to open it.

2

Answers


  1. Chosen as BEST ANSWER

    Based on CommonsWare's answer, and one of his other answers, this is what worked for me:

    In step 1) set the intent action to ACTION_OPEN_DOCUMENT instead of ACTION_GET_CONTENT, and call takePersistableUriPermission()

    val resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult())
             { result ->if (result.resultCode == RESULT_OK) {
                            val data: Intent? = result.data
                            fileUri = data?.data!!
                            this.contentResolver.takePersistableUriPermission(
        filePath, FLAG_GRANT_READ_URI_PERMISSION);
                        }
                    }
        
    val myIintent = Intent()
                    .setType("audio/*")
                    .setAction(Intent.ACTION_OPEN_DOCUMENT)
        
    resultLauncher.launch(myIntent)
    

    In step 2) instead of

    alarmIntent.putExtra("audio_path",fileUri.toString())
    

    do

    alarmIntent.data = fileUri
    

    In step 3) do:

    val fileUri = intent?.data
    val musicIntent = Intent(context, MusicService::class.java)
    
    musicIntent.data = fileUri
    

  2. Replace your putExtra() calls with setData() calls and add addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) to each of those outbound Intent objects. And, replace your getStringExtra() calls with getData() calls.

    This will pass your Uri as an actual Uri in the "data" facet of the Intent, and it will pass along the read permission as you traverse process and component boundaries.

    See this blog post for more.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search