Copying File from Download Folder to ASD

Hello, good afternoon.

I'm trying to copy a file called "contactos.txt" from the Download folder to the Application Specific Directory using Taifum File Extension, but it's giving a permission error. I tried "Read_External_Storage" and "Write_External_Storage", but nothing worked.

image

image

Red this Some basics on Android storage system and use SAF

Taifun

2 Likes

Where does this (non-media) file come from? If it is not created by the app itself you must use SAF to access it.

See also here: How to access non-media & media files on Android 11+.

1 Like

Thank you very much!

I just need to call the copyfile method, or do I need other methods?

image

I'm very sorry to be bothering again, but I really need help :slight_smile:

I'm trying to copy a file from the Dowload folder to the ASD, something as simple as this:

The URI is:

content://com.android.externalstorage.documents/document/primary%3ADownload%2Fcontactos.txt

Is it possible to use SAF without user input?

Best Regards,
Rui

Try

Check your ASD path is correct

Thank you very much.

I'm getting the ASD path through:

FileContactos is the File Component:

image

Is it working now ?

Not yet :frowning:

CaminhoASD = file:///storage/emulated/0/Android/data/appinventor.ai_ugmcp_somincor.ContactosSomincor/files

Error message:
'file:/storage/emulated/0/Android/data/appinventor.ai_ugmcp_somincor.ContactosSomincor/files: open failed: ENOENT (No such file or directory)'.

Remove the file:// from the ASD path

image

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?