SAF: App Inventor implementation of Storage Access Framework

I have been doing more googling. The above suggestion to replace "wt" with "wrt" probably won't solve the issue. That bug was fixed a while back.

However, the following change may be the answer to truncating the file on drive in WriteToFile(final String uriString, final String content)

Explicitly open the file descriptor so that the "wt" is passed on to the drive content provider :

Replace:

OutputStream fileOutputStream = contentResolver.openOutputStream(Uri.parse(uriString), "wt");

With:

ParcelFileDescriptor pfd = contentResolver().openFileDescriptor(Uri.parse(uriString), "wt");
FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());

Adding the following before writing may be more reliable?:

// Use this code to ensure that the file is 'emptied'
FileChannel fChan=fileOutputStream.getChannel();
fChan.truncate(0);

https://stackoverflow.com/questions/56902845/how-to-properly-overwrite-content-of-file-using-android-storage-access-framework

Cheers,
Daryl

@Daryl_Gray

I forked some parts of @vknow360 's SAF extension, and made a saf text doc handling extension.

I have updated it with the code you have supplied above, you are welcome to test it out - you will probably need a test project...

uk.co.metricrat.textdocsafV1.2.aix (15.5 KB)
(this version is not yet on the github link below)
More info about this extension is available on github here

Thank you! I will try this evening and post the result. (Australia)
I also managed to install rush and compile a modified copy of Sunny's code however it does not compile correctly in app inventor....
Cheers Daryl

@TIMAI2 . Unfortunately nothing is written.. Here is a test app.
TestSAFWrite.apk

aia please

In my testing, reads and writes OK for text files on device, reads text file on google drive OK, but does not write it. I think you need google permissions to do this which is outside of SAF, so more blocks needed, and perhaps some google apps script in order to write the file back to google drive.

2 Likes

My version writes to drive however does not truncate even when opening "wt" and calling truncate(0); If I add records, it works as expected, instantly updated on my PC. If I remove rows and save it does not truncate when read by the app or PC however is truncated if I open in android drive.... Think drive has some sync issues.

Maybe I should look for another cloud option.

Thanks for your help!