Miguel Sesma
Miguel Sesma

Reputation: 747

Android 10 - file provider - permission denial

I'm facing a weird issue trying to provide a file in Android 10. My code works fine in Android 9 and 11 but in Android 10 only works 50% of the times. There are no difference on the system status when it works and when it doesn't.

The intent:

    val intent = Intent(Intent.ACTION_VIEW).apply {
        setDataAndType(file, UpdateHelper.APK_TYPE)
        addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    }

If I grant the permission explicitly, it always work. So my question is not how to fix it, but if there are any explanation for it that I'm missing. In my understanding granting the permission in the intent is the preferred way of doing it.

context?.grantUriPermission(packageName, file, Intent.FLAG_GRANT_READ_URI_PERMISSION)

Upvotes: 2

Views: 223

Answers (1)

Miguel Sesma
Miguel Sesma

Reputation: 747

Reviewing the documentation, it seems to be a race condition.

Permissions granted in an Intent remain in effect while the stack of the receiving Activity is active. When the stack finishes, the permissions are automatically removed.

For some reason this does not affect Android 9 and 11. But in Android 10 does. The reason for my app to be finished is that I'm using this to pass a new apk to the packageInstaller in order to update the app.

Upvotes: 1

Related Questions