Unable to read file only when running code as Tasker task

1,318 views
Skip to first unread message

Diederik Boersen

unread,
May 16, 2021, 9:53:06 AM5/16/21
to Tasker - Developers

I'm having trouble helping out a user of my plugin (PdfToText).

With the plugin you can select a Pdf file which will be loaded as text in Tasker.

The configuration screen has a test button which reads and shows the content of the file just fine. Also, when this user adds a "Read File" action to this specific file Tasker is able to read the (raw) content.

However, when the user tries to execute the PdfToText task it results in the error:
Permission Denial: opening provider com.android.provider.media.MediaDocumentsProvider from ProcessRecord … requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs 

The code I use to obtain the file uri is

val intent=Intent(Intent.ACTION_OPEN_DOCUMENT).apply{
    
addCategory(Intent.CATEGORY_OPENABLE)
    type="application/pdf"
}
startActivityForResult(intent,PICK_PDF_FILE)

 As far as I know this issue occurs only on Android 11 (API 30) but I'm unable to reproduce this on any VM.

 I'm guessing there is something wrong with the way I obtain the file uri and use it in the Tasker task. Would a different way of obtaining the file uri be the solution and if so, what would be the correct way?

 Any help is appreciated very much.

Falco Schaffrath

unread,
May 16, 2021, 10:52:10 AM5/16/21
to task...@googlegroups.com
You cannot share the permission provided by ACTION_OPEN_DOCUMENT so easily.
You can only share the permissions with the intent started by your app (startActivity). (I don't remember how the Flag is called to share the permission with the started intend, but doesn't matter because this doesn't work with the tasker plugin api and not sure which types of intents does support it)

If Tasker opens your app (like it does because this is how the plugin interface does work) you will not have the permission because Tasker doesn't store the permission for your forever.

One trick you can do, is to store the permission inside your app:




--
You received this message because you are subscribed to the Google Groups "Tasker - Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to taskerdev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/taskerdev/9e05a6b9-c7d4-4091-94f0-0aed2b11fb57n%40googlegroups.com.

Falco Schaffrath

unread,
May 16, 2021, 11:09:32 AM5/16/21
to task...@googlegroups.com
If this flag is supported by startActivityForResult Tasker would be able to get your permission during task configuration. But this is still not enough because you need this permission during runtime. Even if tasker would store your permission and keep it alive for the next Task run, it will not work after restarting Tasker. I can imagine that the only possible way to do it is to open the file pick dialog in your app and store the permission in your app. According to documentation this will be usable even after app restart. But you would need to open the file pick dialog every time the user does restart the device. So in the end you have to implement "pick file on receive tasker call" anyway to solve this "permission is missing after device restart". Knowing this I am not sure if https://developer.android.com/training/data-storage/shared/documents-files#persist-permissions does help you at all. Maybe it's best to open the file pick dialog every time you want to read or write something.

You can use DocumentsContract.EXTRA_INITIAL_URI to help the user finding the correct file.

Diederik Boersen

unread,
May 17, 2021, 11:25:31 AM5/17/21
to Tasker - Developers
Hi Falco,

Thank you very much for your help. I was hoping there was an alternative way of calling the api's that would make it possible to read the file just as Taskers Read File action does. But if I understood correctly this is not possible for plugins without showing file pick dialog on occasion.

I think I'm going to extend the plugin to support parsing pdf from Tasker variables. This way, the user can split the parsing of a Pdf file in a Read Binary and PdfToText action. The user will have configure an extra action but will keep the tasks fully automated. Also, it will allow to read pdfs from different sources.

Thank you again for the explanation. It's still hard for me to get a grasp on the file permission system of Android 11 but your explanation made me understand it a little bit better now :)

Op zondag 16 mei 2021 om 17:09:32 UTC+2 schreef Falco:
Reply all
Reply to author
Forward
0 new messages