SAF: App Inventor implementation of Storage Access Framework

Did you read the examples provided in the topic?

4 Likes

because a fileName like /My Folder/Appunti.txt is not an uriString
Taifun

3 Likes

can you explain better ?

2 Likes

See this post above

2 Likes

All my attempts have failed, can modify my blocks ?

2 Likes

I'm trying to copy a mp3 file from the ASD to the /Ringtones directory
here are my blocks

I get the following error message

Permission Denial: writing com.android.externalstorage.ExternalStorageProvider uri content://com.android.externalstorage.documents/tree/primary%3A/document/primary%3ARingtones from pid=13158, uid=10721 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()

what needs to be changed?
also how can we find out, if the TakePersistableUriPermission method was successful or not?
Taifun
PS: if this works I will try to copy the file from the assets to the folder /Ringtones...

3 Likes

Probably the writing method.
Initial version writes/reads content as string, which works only for text/* files.
But v1.1beta enables you to directly write content as byte array. @Anke
https://github.com/vknow360/SAF/tree/beta

Here, Label1's text was set to created document's URI i.e. content://com.android.externalstorage.documents/tree/primary%3APictures/document/primary%3APictures%2FCompanion.png.
treeUri is content://com.android.externalstorage.documents/tree/primary%3APictures and documentUri is content://com.android.externalstorage.documents/tree/primary%3AAndroid/document/primary%3AAndroid%2Fdata%2Fedu.mit.appinventor.aicompanion3%2Ffiles%2Fassets%2FCompanion.png.

CopyDocument block seems to be not working for /Android subdirs.

Using these two blocks:

image

This is still missing from the extension :sweat_smile:

5 Likes

You might want to read the above replies again. :slightly_smiling_face:

2 Likes

Thank You very much for your big help... :cry:

2 Likes

You cannot open / pick from the ASD on Android 11 (because this dir is no longer visible on Android 11), also not unsing SAF.

But if the fileName is known (in my example loop.ogg) you can copy it from the ASD to the Shared folder /Ringtones (of course without WRITE permission):

grafik

If the fileName in unknown, you can list all relevant audio files from the ASD and pick the sound file from a ListPicker.

4 Likes

Thanks, I see it works with media files, but I also need pdfs and some doc/docx.

2 Likes

thank you,
and how would I have to configure the parameters to make it work:opensinglefile

2 Likes

title can be anything.
category must be android.intent.category.OPENABLE .
type is the mime type of what you want to browse. For pdf it should be application/* and extraMimeType should be a list of acceptable mime types such as application/pdf in this case.

Selected document's uri will be returned in GotUri method.

3 Likes

then it would work like this:

it did not let me place the extraMineTypes block.

2 Likes

That parameter accepts a list.
Also, you will get uri from that event so there is absolutely no need to reassign the wrong value.

2 Likes

I'm sorry I'm not an expert, I don't understand very well where to put the file path.

1 Like

as far as I understand, the OpenSingleDocument method lets you select a single document (in your case the pdf document)

OpenSingleDocument
Prompts user to select a single file

so what happens after using that method and selecting that file? which uri do you get in the GotUri event? that uri you have to use to for example display the pdf

Taifun

2 Likes

Thank you very much, now I understand!
This block configuration works. I can select a pdf file and I can see the uri in the label I have put. The problem I have now is when viewing the pdf. I pass the uri to activitystarter, it opens the pdf application, but does not open the file.

1 Like

you can try to convert the uri into a filename and try that filename with the activity starter... my file extension offers the method GetFileName for this https://puravidaapps.com/file.php
in case that method does not work for you (because actually it was created to Get path and filename from media content uri), you also can use the corresponding method from the filetools extension by @vknow360 https://community.kodular.io/t/filetools-some-tools-to-work-with-files/40051

probably it would be nice to have such method in the SAF extension directly?

btw. to simplify your blocks you can move all blocks from the Button2.Click event into the GotUri event...

Taifun

1 Like

Thank you Taifun,
I tried converting the uri to a file path with your extension and it didn't work, but it did work with Filetools as you told me.
Also reducing the blocks will work fine for me, just testing now, to see the label I put it like this.

Still, I can't get the pdfs to open with SAF. Maybe it's because of what @vknow360 was saying earlier. I don't know.
I am attaching the blocks for anyone if anyone sees any errors in them.