Copying File from Download Folder to ASD

Thanks! I Also had to include the file name in "dirpath":

so dirpath is now:
/storage/emulated/0/Android/data/appinventor.ai_ugmcp_somincor.ContactosSomincor/files/contactos.txt

The error message is:
'Permission Denial: opening provider com.android.externalstorage.ExternalStorageProvider from ProcessRecord{95a49dd 24119:appinventor.ai_ugmcp_somincor.ContactosSomincor/u0a205} (pid=24119, uid=10205) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs'.

This works OK for me

You should not need to include the filename in the dirPath

Note: FilePicker uses the intent: android.intent.action.OPEN_DOCUMENT

I've tried your code and it worked.

Can you try doing it without the File Picker? Just put the content URI directly into sourceURI.

Just remove the File Picker from the project.

It makes no sense, but it only works if you call File Picker First.

Rui

It has something to do with this:

FilePicker uses the intent: android.intent.action.OPEN_DOCUMENT

...requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs

This is all you need:

Hello Anke.

Yes, but I want to do it without any user interaction. I want to copy the file automatically every time someone opens the App.

I've tested and if I open the file first with OpenSingleDocument or FilePicker, then it works perfectly.

I tried to use ReadFromFile and the error message is:
...requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs

Apparently the file needs to be opened 1 time with FilePicker or something similar, and then everything works fine.

Try doing your example without using OpenSingleDocument , just put an URI into "sourceuri"

There is no way to do it that way (wthout declaring and granting MANAGE_EXTERNAL_STORAGE permission).

Also the contenturi is probably guaranteed to be different on different devices, and it may change if the file is being updated.

It may be possible if you convert a file path, e.g. /storage/emulated/0/Downloads/test.txt to a contenturi ?

Been doing some reading, it may be possible once a user has selected the file and set permissions to that file:

ref (read to the end)

So on first run of the app, get the user to go off and pick the file, TakePersistableUriPermission for the contenturi, then store the contenturi for subsequent use. Have a check in place if the automatic method fails, so that the user can go off and pick the file again.

As said above not sure what happens when the original file is updated/replaced, you will need to test this (and please report back)

If the user has done this once at the beginning, he already has full access to the file and can of course, as I have shown, copy it to one of the Shared folders or the ASD. After that, the app has full access to this file without any permissions, since it created this file itself.

These statements (regarding the Shared folder) of course only apply to Android 11+.

We have perhaps not yet asked the right question. Where does this text file come from, and how does it get onto a users device. Is it created/updated by another app?

Post #3

1 Like

Hi guys,

First of all thank you very much for taking the time to help me.

So, I have developed an App that updates the phone's contact list based on the content of a text file.

We used Airwatch (MDM software) to transfer the text file to the ASD, but after Android 10 this is not possilble anymore. Airwatch can only access Shared folders, like Documents or Download.

So my idea was to trasnfer the text file to one of these folder and let the App copy it to the ASD or read it directly from the Shared Folder.

But apparently for this to be possible with SAF there must some user interaction, to let the user select the file, at leats for the first time.

So I think I will try MANAGE_EXTERNAL_STORAGE.

Which will be OK as long as you do not want to publish the app on Google Play.

Also, there are extensions available that may manage the contacts lists ?

e.g.

Hello,

Yes, I'm already using ContactsSaver. Perhaps you can give me some help regarding this piece of code

How do I know if the User granted the permission or not?

Use these blocks to solve your basic problem.
ContactBlocks

Hi.

That works for permissions that are asked using ActivityStarter?

You could try using this extension.

You can try that also.
As Ask permission also have permission for Read Contacts.

1 Like