SAF: App Inventor implementation of Storage Access Framework

1.Introduction

Description: Basically AI2 wrapper of Storage Access Framework. Using this extension, you can get read and/or write access to a single document or tree document (and all of its children recursively).
Latest Version: 1.1
Released: 2021-09-16T18:30:00Z
Last Updated: 2021-11-01T18:30:00Z

2.Blocks

image




image
image
image

3.Documentation

Docs for: SAF

Events

DocumentCreated

Event invoked after creating document.Returns document's uri if operation was successful else returns error message
Params

uriString | text


ErrorOccurred

Event indicating error/exception has occurred and returns origin method and error message.
Params

methodName | text
errorMessage | text


GotCopyResult

Event invoked after getting copy document result.Response will be target document's uri if operation was successful else returns error message
Params

successful | boolean
response | text


GotFilesList

Event invoked after getting files list
Params

filesList | list


GotMoveResult

Event invoked after getting move document result.Response will be target document's uri if operation was successful else returns error message
Params

successful | boolean
response | text


GotReadResult

Event invoked after reading from document.Returns content if operation was successful else returns error message
Params

result | any


GotUri

Event invoked when user selects a document or tree from SAF file picker
Params

uri | any
uriString | text


GotWriteResult

Event invoked after writing to document.Returns document's uri if operation was successful else returns error message
Params

response | text


Methods

BuildChildDocumentsUriUsingTree

Builds child documents id using tree (documents which is child of parent document) uri and its parent document id
Params

treeUri | text

parentDocumentId | text

Return type : text


BuildDocumentUriUsingTree

Builds document uri using tree uri and document id
Params

treeUri | text

documentId | text

Return type : text


CopyDocument

Tries to copy document from source uri to target dir
Params

sourceUri | text

targetParentUri | text


CreateDocument

Creates a new and empty document.If document already exists then an incremental value will be suffixed.
Params

parentDocumentUri | text

fileName | text

mimeType | text


CreateFlags

Combines two flags and returns resulting flag
Params

f1 | number

f2 | number

Return type : number


DeleteDocument

Tries to delete document from given uri and returns result
Params

uriString | text

Return type : boolean


GetDisplayName

Returns display name of given document uri
Params

documentUri | text

Return type : text


GetDocumentId

Returns document id of an uri (should only be grand child)
Params

uriString | text

Return type : text


GetLastModifiedTime

Returns last modified time (epoch) of given document uri
Params

documentUri | text

Return type : text


GetMimeType

Returns mime type of given document uri
Params

documentUri | text

Return type : text


GetSize

Returns size (in bytes) of given document uri
Params

documentUri | text

Return type : text


GetTreeDocumentId

Returns document id of tree uri (should be either tree uri itself or a direct child uri)
Params

uriString | text

Return type : text


IsChildDocumentUri

Returns whether second uri is child of first uri
Params

parentUri | text

childUri | text

Return type : boolean


IsCopySupported

Returns whether document can be copied or not
Params

documentUri | text

Return type : boolean


IsDeleteSupported

Returns whether document is deletable or not
Params

documentUri | text

Return type : boolean


IsDocumentUri

Returns whether given uri is a document uri
Params

uriString | text

Return type : boolean


IsMoveSupported

Returns whether document is movable or not
Params

documentUri | text

Return type : boolean


IsReadGranted

Returns whether read is available for given uri
Params

uri | text

Return type : boolean


IsRenameSupported

Returns whether document is deletable or not
Params

documentUri | text

Return type : boolean


IsTreeUri

Returns whether given uri is a tree uri
Params

uriString | text

Return type : boolean


IsWriteGranted

Returns whether write is available for given uri
Params

uri | text

Return type : boolean


ListFiles

Tries to list files from given dir
Params

dirUri | text

dirDocumentId | text


MoveDocument

Tries to move document from source uri to target dir
Params

sourceUri | text

sourceParentUri | text

targetParentUri | text


OpenDocumentTree

Prompts user to select a document tree
Params

title | text

initialDir | text


OpenSingleDocument

Prompts user to select a single file
Params

title | text

type | text

extraMimeTypes | list


ReadAsByteArray

Reads content of document as byte array
Params

uriString | text


ReadFromFile

Reads from given document as text
Params

uriString | text


ReleasePermission

Relinquish a persisted URI permission grant
Params

uri | text

flags | number


RenameDocument

Tries to rename a document and returns updated uri
Params

documentUri | text

displayName | text

Return type : text


StringFromUriObject

Convert uri to string
Params

uri | any

Return type : text


StringToUriObject

Converts string to uri
Params

uriString | text

Return type : any


TakePersistableUriPermission

Take a persistable URI permission grant that has been offered. Once taken, the permission grant will be remembered across device reboots.
Params

uri | any

flags | number


WriteAsByteArray

Writes byte array to given document
Params

uriString | text

byteArray | any


WriteToFile

Writes to given uri
Params

uriString | text

content | text


Properties

DocumentDirMimeType

Returns mime type of document dir

Property Type : read-only
Accepts : text


FlagGrantReadPermission

Flag to get write permission

Property Type : read-only
Accepts : number


FlagGrantWritePermission

Flag to get read permission

Property Type : read-only
Accepts : number


4.Usages

1.Open document tree
image

2.Get access (not necessary for one-time usage)

image

3.Build child document uri

  1. if document is direct child

  1. if document is grandchild

4.Read file/Set picture
image

5.Download

Aix:
com.sunny.saf.aix (30.6 KB)

6.Liked my work/Want to donate :heart:

Thank you :hugs:

7.ChangeLog

ChangeLog Version 1.1

New Blocks
image
Changes

  • Removed category param from OpenSingleDocument method
  • GotCopyResult and GotMoveResult now return resulting/target document’s uri

Bug Fixes

  • Fixed issue in write methods
  • Fixed null pointer exception when SAF picker was cancelled by user

Hope it helps! :slightly_smiling_face:

40 Likes

Didn’t understand anything :cold_sweat: . why should we use this extension instead file component. What is document tree

That’s not a problem.
You can always google it.

People interested in Storage and Developers working with files will find it useful.

1 Like

I want to know, about it work

I Am Just Shocked How Much Hardwork You Have Done In Making Extension

specially In Creating So Long Post With Proper & A to Z Documentation.

Its Took Me around 10 Sec to scroll to view comments

A Big Salute to you :love_you_gesture: :love_you_gesture: :+1: :+1:

Really You Are Masterpiece

Awesome Extesnions

6 Likes

What an amazing extension! Great job!
You are far ahead of AI2. :wink:
I’ve only done a few tests and it seems to be working so far.

6 Likes

@vknow360 excellent work! thank you very much! :partying_face:
I guess, you also will publish in the App Inventor community soon…

see the overview by @bodymindpower

everywhere you can see Storage Access Framework you can use it… these are places the file component can’t access… for example to read or write non media files created by other apps

Taifun

7 Likes

Thank you :blush:

Already did, waiting for approval.


It will work in Android KitKat and above versions but it is recommended to use SAF on Android 5.0+.

1 Like

can it work in android 11?

Done …
It’s a shame that your extensions need approval in AI2.

11 Likes

We are facing problem with the file component of kodular after the new update of kodular with android 11 phones
I am just creating a file in the external storage and retrieving it from the same place as per the requirement
Does it will solve the issue?

Excellent work :+1: :clap:

2 Likes

starting with SDK30 you should not create arbitrary directories anymore…
use the ASD or shared storage like /Download
see also the overview by @bodymindpower

Taifun

2 Likes

Epic work @vknow360 :love_you_gesture:

2 Likes

My app is a offline app, which stores salary statements of the employee. I have included a tab to export the stored data to the phone to provide the user to transfer data to new phone in case the user is changing the phone.
I was storing the file directly to the external storage “/storage/emulated/0/salary”. Now this thing is not working in the new kodular update.
Pl suggest me if i want to use this extension then how to do this.


blocks(25)

@Shreyaa
I dont know how to use this

If you can help. I need to create the file (if possible any where in phone) and save data in that

bruh why you @narender_kumar mention me ? ask from @vknow360 he is the developer of extension

Read the post and show the block what you have tried till now

@vknow360

I was having little bit problem to understand how it works

In .OpenDocumentTree block what I have to add in it

I added this /storage/emulated/0/Android/data path in OpenDocumentTree

When I call the block it show the /storage/emulated/0/

Actually I was trying to access text file in this path /storage/emulated/0/Android/data/com.OtherAppPrivate.Dir/files/text.txt

And trying to modifying it

Why you put the “/storage/0001-61E9” in that block

I am very confused about SAF

SAF can give you access to 90% of the file system.
But you can’t access /Android/data and /Android/obb dir and its childs.

That’s path of sdcard in my phone. :slightly_smiling_face:

Example 2

1 Like