Reading file data from a browsed local file

Hello, I'm trying to build an app where the user can browse the file manager and send any file (formats CSV and GPX) through bluetooth, to another device (esp32).
Here's what I think I should do:

  1. allow the user to access the file manager and select the file (Blocks attached)
  2. read the file's data and convert it to the desired string
  3. send the string through bluetooth and process it on the other device

I'm currently stuck on part 2 (read file's data), as I've not found a way yet to view the file that has been selected from the file manager. Everything I've found on the web refers to reading file data from a specific file that has been uploaded as media to the app inventor. In my case, I'd like to access the data of the file that the user selects (browses from the file manager).

How can I do it (if it's possible)? for the CSV format, for instance.

Any help would be greatly appreciated. Thank you for your time!

If it is a text based file, use the built-in File component to read the file.

I've tried to test a few things on a txt file, using the built-in File component in the storage section. I got the error "File not found" and I never managed to fix it. here are the blocks:

Although that might work, I'd still have to read the file's data of a non-text based file. specifically, the GPX format. (I might be able to work with CSV similar to the txt, but I haven't gotten the txt format to work yet).

Any help please..? I'm still stuck trying to get it to work..
I've found no way to read a file's data that has been browsed from the file manager.
I've tried the built in File component, taifun's extention, and some others.. :frowning:
Thanks..

what is an example of the 2 global variables FilePath and FileName you are using there?
to find it out, just display it in a label...

Taifun


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

Those are just to help me debug what's going on. I only use them to display them in a label afterwards (picture included below).

Here's what I'm trying to do:

  1. The user browses a file from the file manager. here are the blocks to do this:
    image
  2. Now, I'd like to get the data that's in the selected file (the file that the user selected when browsing), Let's say the file is in .txt format, or .csv, I'd want to be able to read the contents in the file, in order to store them in strings that I will use later on..

I'm stuck on this part (2), I can't view the file's content, the "ReadFrom" function is not working for some reason.. I've tried many extensions and sadly I can't seem to get it to work..

Any help is much appreciated! Thanks!

what about providing an example filepath and filename as previously suggested?
we then can take a look, if you are reading the file correctly from there
also which Android version are you using for your tests?

http://ai2.appinventor.mit.edu/reference/components/storage.html#File

ReadFrom( fileName )

Reads text from a file in storage. Prefix the fileName with / to read from a specific file on the SD card (for example, /myFile.txt will read the file /sdcard/myFile.txt ). To read assets packaged with an application (also works for the Companion) start the fileName with // (two slashes). If a fileName does not start with a slash, it will be read from the application’s private storage (for packaged apps) and from /sdcard/AppInventor/data for the Companion.

Taifun

I'm using Android 11, on a Galaxy S20 Ultra (and on a Galaxy s8).
Here are a few examples:
When I use only this block,
image
this is the ResultUri that I get:
image
As you can see, the file name is numbers (the actual file name is "TestRoute.txt", but it could be any other random name, depending on the user of the application. so, there isn't a specific file name, I need to extract the name in order to call the "ReadFrom" function)

If I use your extension to get the file name from the Uri, I get this Error:
image


It seems that the "GetFileName" function is returning nothing.

Thank you for your help!

you might want to try the corresponding method from the FileTools Extension by @vknow360

Taifun

I tried, it appears in the original block (first post up there).


This block is not giving an error, but it's not working. It's leaving the Label empty..

alternatively you could try a file picker extension...
the question is, if these still will work after App Inventor tagets SDK30...

taken from the extensions directory https://puravidaapps.com/extensions.php

Taifun

This block works. The weird thing is that it's working for literally any other file that I choose, other than a .txt file..
Here are a few examples:
This "unknown-3.png" image is sitting in the exact same folder as the "TestRoute.txt" file that i'm trying to view.


It does show the file path correctly.

Another example for a file with a different path:

And finally, the "TestRoute.txt" file that I'm trying to view.. it leaves the label empty..

You may need to try this, which works for me on Android 10 (companion). The File component could be looking at the ASD..., so you may have to copy the picked file there (ASD) first in order to be able to read it.

For some reason this did get the path and name correct, but it still could not find the file (the "ReadFrom" function is returning this error:)
image

Here are the blocks (which I copied from you)

Is it because of andriod 11?
Thank you for your help!

Blocks are not quite the same because you are adding an extra .txt to the end of the filename

True, I fixed it and it still doesn't work (Edited the post above)

you are using an Android 11 device
currently the file component only can read from the ASD (and not from a random directory) starting from Android 10
therefore follow this advice

this might change after App Inventor targets SDK30...
for details see this thread Testing the next release of MIT App Inventor (nb187) - #37 by ewpatton

Taifun

You also didn't put a / in front of the filename.....

I have tried to copy the file to the ASD, but even then, the "ReadFrom" function doesn't work..
I started a fresh project with just these blocks and to test it, I still get the error that the file was not found.
I checked if the file was copied successfully, also, I'm displaying the new file path on another label
Here are the blocks:

The result:

I don't quite understand what the SDK30 is and what it will do.. is there anything I can do to get it to work in my current situation or do I have to wait for the new release?

Thank you @Taifun and @TIMAI2 for your help!

your blocks look fine...
which response do you get in the FileTools.FileCopied event?
is the file really there in the ASD where you expect it?

it will bring heavy restrictrions on how to access files in future...
see also @ewpatton's bog here, which has been published 1 year ago
https://appinventor.mit.edu/blogs/evan/2020/08/08/file-path-updates-android-10
which means in your case, probably without using an extension (which still has to be written) it then is not possible anymore to access a file in the /Download folder if your app did not create it, see also the discussion here Testing the next release of MIT App Inventor (nb187) - #187 by Taifun

Taifun