Saving files 0 bytes

I'm having trouble saving strings to a text file. I pick the dircetory/filename using the filepicker. Then upon saving, something happens, but all it does is save a 0-bytes file. Is there anything obvious I am missing?
Attaching screenshot of the relevant code... I don't think the replace part matters actually, I've tried many things.

Why is there a red error?

Use the companion app and Do it to debug your blocks, see also tip 4 here App Inventor: How to Learn | Pura Vida Apps
see also Live Development, Testing, and Debugging Tools

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by icon24 Taifun.

Ignore the error, I duplicated the relevant block and removed irrelevant processing of the actual string I want to save. Thanks for the tips but I tried already the tips you recommend

So what is your Do it result? Show us a screenshot including Do it result
Also how did you set the file scope property of the file component?
Taifun

OK forget about Do it. Here is a clean app with no block errors. I tried file scope "app", "shared", "legacy". Shared gives an error, whereas "app" and "legacy" save a 0-bytes file. May be a permission problem but why would it save a 0-bytes file? The other issue is that when I print out the file name to show on the screen, I get some extra characters, e.g. picking aaa.txt results in:

file:///com.android.externalstorage.documents/document/primary%3ADownload%2Faaa.txt

i.e. I don't know if it's those % extra characters creating an issue

What makes you think that changing the content: to file: is going to work? This is not the way to convert a contenturi to a filePath...

What about storing the text always in a file in ASD without using the filepicker? Then later you could copy the file to shared storage and ask the user to provide a name for the file. .

Taifun

Or do this instead:

image

Changing content: to file: seems to be necessary, otherwise the app crashes when trying to save.

One issue is those encodings, replacing %2F with / and %3A with : then I can manage to save the file somewhere and retrieve it. However, I am not sure where it is, it doesn't seem to be saved in the directory I picke - looking through the directory tree with a file manager app does not find it

This code saves and retrieves text properly, but it's not clear where

The file will be lost in a directory structure in your ASD

Then what is the proper directory name of a shared directory? It is not the one loaded by the file picker

You are not setting the filescope for the file component correctly to access the shared directories

Trust me, I tried all filescopes. The main issue is finding the correct directory name. It is not the one loaded by the file picker.

Get the file saving to the correct place first, then worry about picking a directory.

If you really want to do that, then what about using a file extension to convert the uri into a path correctly?

Taifun

1 Like

Thank you! Really helpful extension