How do you create a new file using file picker and write to it?

It is trange though, that the uri is OK for reading the file but not OK to write into it.

How about using the Sharing component?

You could throw the file into the air, and let the user catch it however he wants.

you are on Android 9
I used Android 15

Taifun

this is not strange, because you do not own the file and therefore do not have write permission
try it with a file your app owns (i.e. pick a file you previously stored using the SaveFile method) to see the difference

Taifun

As for using sharing, it is OK (and I know that these days most people use android devices continuously connected to the web), but if it can be done without it, then why not?

As for accessibility, from what I read in the documentations and in the guides, it should be OK to read files from the Downloads and Documents folder of android as long as the file scope is either legacy (which I use) or shared. The guides also say that if the file is created by the app (which is true in this case), then it is also writeable in these folders. Do I understand this correctly?

Can you please try my version that works for me on Android 9?
Here is the file:
readWrite(1).aia (2.9 KB)

The file is created by the app using the file picker. I am not trying to write in a file that was created with another app.

it is not true in this case, because it is not your app which creates the file, it is the filepicker dialog, which seems to be outside of the app

Taifun

I can try to save a file, but I will not see the difference because I use Android 9 and the file picker approach also works for me.

it looks like you have to test this on a newer device to see the storage restrictions for newer devices
Taifun

Yes, I know.
I am now trying on an Android 10, this is the newest I have.
On this one the uri is of a different format, I need to experiment with it to get the file path out of it.
It would be nice to know what the syntax of an uri for a file is, but I could not find any documentation about it.
Is there by chance a resource where I can find how the app inventor handles it in the "read file" block?

you should test on Android 13+

do you plan to publish your app in Google Play?
If not you also can follow this suggestion

Taifun

the sources are here

Taifun

here is the result

the uri decode method of web component is not able to convert a content uri into a filepath
try one of the file extensions

EDIT: also I think, it is not necessary to do the conversion. If you own the file it theoretically should be possible to store it using the content uri...

EDIT: no, I was wrong, it produces the same runtime error as in your first post of this thread

example project attached
readWrite3.aia (64.1 KB)

  1. create file
  2. read file using the file picker
  3. save it using the provided content uri

Taifun

Thank you for the sources, I will check this out.

Yes, I read the guidance you linked and yes, I am planning to publish the app, so I do not want to ask for writing permission.

I do not want to use extensions, I want to keep it open to also publish on iOS

I will try to do some workaround and maybe use the file picker to find the directory and use the save option to create an empty file by the app before I fill it up with content.

If there is an example somewhere where I could see how to handle the file picker to look for directories, it would be helpful. Otherwise, I will need to experiment to see what the return value of the file picker is in the "select directory" mode.

It was new to me to hear that whatever the file picker creates from the app is not considered as created by the app, but that's life, I need to work around it.

Thanks for the help.

This is why I was surprised that this is simply not the case. On my Android 9, using the uri did not work when I tried to write in the file (see my code in my starting post), but I could write in the file after conversion.

I was wrong, see my updated answer

@ewpatton why is it not possible to use the file component and the SaveFile method together with a content uri provided by the file picker to store a file the app owns?

java.lang.IllegalArgumentException: URI scheme is not "file" at java.io.File.(File.java:429) at com.google.appinventor.components.runtime.util.ScopedFile.resolve(ScopedFile.java:65) at com.google.appinventor.components.runtime.File$9.processFile(File.java:628) at com.google.appinventor.components.runtime.util.SingleFileOperation.performOperation(SingleFileOperation.java:163) at com.google.appinventor.components.runtime.util.FileOperation.run(FileOperation.java:135) at java.lang.Thread.run(Thread.java:1119)

Taifun

Thank you for trying. As I said, I will do some workaround, but in your example the program did not even get to the point to try to write in the file, because the split of the uri did not give the file name. On your system a different split would be needed, but it is not worth trying it, because first of all, I beleive you that it will not work, and secondly, if the uri is this different on every system, thne there is no point trying to find a universal way of getting a file path out of it (although the app inventor file reading block manages to do it).

You could try with the SAF extension?

See HERE, scroll to Section 4 (Working with Text/CSV files in Shared Directories) of the guide.

Should work with all android versions.

Since I would like to keep it open to be available for iOS, I would like to stay away from extensions for the moment (although I do use the BLE extension to communicate with a microcontroller in the hope that at some point it will not be a problem for iOS either).

Is this a bug? I could not so far figure out a universal way of getting the file name and path out of the uri, it would be good to be able to write to a file where the user can look for the path, even if it is in a restricted location.