SAF: App Inventor implementation of Storage Access Framework

And again:

So please answer this question: Does it work for you with my test app?

Your app works well in my phone, I copied your text files into / Documents, I was able to list / read / delete them, but I'm not able to create my app using your blocks. I don't want list the folder, I want open a single document, read it and save changes.

I believe the simplest way to do that is:

1 Like

Thank you, this code runs well, but there is a way to avoid picking list ? Now I know the uristring of my file that I want read.

Is the app just for your own use?
If not, how do you know the file exists on the user's device? In other words, where did this file come from?

yes, the app is for my own use, I'm not a developer, I know the file exists because I put it in my phone. I tried reading the file using the uristring, but I think there is a permissions problem

Show your blocks.

@vknow360 The function "Writes to given uri" apparently no longer works in the beta version of your extension. The doc is created, but nothing is written in it: "No content provider: ...".
This worked before as I showed in a video.

Java
    @SimpleFunction(description = "Writes to given uri")
    public void WriteToFile(final String uriString, final String content) {
        AsynchUtil.runAsynchronously(new Runnable() {
            @Override
            public void run() {
                if (!GetMimeType(uriString).equals(DocumentDirMimeType())) {
                    String res;
                    try {
                        OutputStream fileOutputStream = contentResolver.openOutputStream(Uri.parse(uriString));
                        res = writeToOutputStream(fileOutputStream, content);
                        res = res.isEmpty() ? uriString : res;
                    } catch (Exception e) {
                        res = e.getMessage();
                    }
                    postWriteResult(res);
                } else {
                    postError("WriteToFile", "Can't write text to dir");
                }
            }
        });
    }

    private String writeToOutputStream(OutputStream fileOutputStream, String content) {
        OutputStreamWriter writer = null;
        try {
            writer = new OutputStreamWriter(fileOutputStream);
            writer.write(content);
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        } finally {
            try {
                if (writer != null) {
                    writer.flush();
                    writer.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "";
    }
1 Like

It was working when I tried it last night.

It means file is not there or it failed to open output stream.
Can you send an apk to test?

1 Like

uristring : content://com.android.externalstorage.documents/document/primary%3AMyFolder%2FAppunti.txt

Yes my mistake, but see here:

Blocks

Try this:

content://com.android.externalstorage.documents/document/primary%3ADocuments%2FmyFolder%2FAppunti.txt

Fixed :smile:

Two issues has been fixed:

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

Checked, works. Thanks.

1 Like

No good, no errors, no read file.
The fullpath is /storage/emulated/0/MyFolder/Appunti.txt

Updated:
Saf_readWrite.aia (60.6 KB)

1 Like

Thank you Anke, but your aia does not run in my phone, when i read the app stops, when I save permission denied.

What does this mean? Did you get any (error) message?

Writing
No content provider: Permission Denial: opening provider com.android.externalstorage.ExternalStorageProvider from ProcessRecord{2aadb0 26783:appinventor.ai_andrea_patelli.Saf_readWrite/u0a481} (pid=26783, uid=10481) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs

Reading
No content provider

Did you take persistable uri permission before accessing document?

1 Like