File - Can't Access Files Outside AppInventor/data folder

I can read a file from AppInventor/data folder (in Internal storage). I can't read a file from another location.

If the fileName does not start with a slash, it will be written in the program’s private data directory...

Since writing can be done only to a specific directory, I assume the same applies to reading and so not being able to read from other file system locations is expected. Is this correct?

Hi Petr

Not correct. You cannot read from another App's protected data space, but you can read-write from-to the 'public' Virtual SD memory storage area (called SD Card in the help):

1 Like

Ahh... okay. Thank you.

I'd like to tail a file but ReadFrom always returns contents of the entire file and there is no seek parameter or function. So no hope on tailing a text file? Or is there some clever workaround perhaps?

The File component can read from all directories of internal and external storage, but not write to all.
It cannot be written in:

  1. assets (→ internal storage)
  2. external (removable / mirco) SD card (since Android 4.4 / KitKat).

See also here:

1 Like

Awesome! Thanks.

By 'tail' do you mean append?

1 Like

Tail is a bash command in linux:

I see - never seen that before. Would this do it?:

Snap2

My original problem and question was about reading data from outside of AppInventor/data/ directory. Thanks to Anke I now have a better understanding of the filesystem but I still could not read from outside locations. The error is about file not existing so I think something is wrong with my path (I tried all sorts of variations, including path shown by file manager in details.

To proceed, I modified the application (not AI2) that generates the file so that it puts the file into AppInventor/data/ folder. So my question still remains but I have a workaround. :slight_smile:

Next, I read the file and it worked fine. Now the thing is that the other app keeps writing (appending data) to the file. In my AI2 app, I would like to read only any newly arrived data. However, ther read operation on file objects has no seek/position parameter. I can only read the entire file, every single time. This is okay for a while but as the file grows, reading the entire contents again and again (just to get any newly appended data) becomes slow.

So, Chris, yes, your suggestion works. One could also keep a copy of the previous read operation and see how the old and the new differ (at the end) if at all. I cannot do this because the file keeps growing the reading it repetitively at high rate becomes prohibitively slow.

This is where I asked about tail. Timai, yes, I wanted to tail'n'follow ("tail -f") the file, or find some way to seek to a position before the read operation. Since this is a different question, I spinned up a new thread. It got closed but in it, Peter suggested a "Linux command" extension. And it almost works. I can execute Linux commands and get back stdout, I have tail on my system. Unfortunately, I then get back to the original problem. I am not sure what the working directory of the run method is. It is not AppInventor/data/ I tthink. And, unfortunately, I still do not know how to point to a file in AppInventor/data/ or in a directory of another application.

I will now end by pasting from the other thread to show what I can and cannot do when running Linux commands. Particularly, ls shows only some directories/files (perhaps due to their ownership). If only I can point tail at the correct [path-to]/AppInventor/data. :frowning:

Pasted:

I can run "echo aha" --> "aha"
"uname" --> "Linux"
"ps" --> "... info about one process ..."
"tail --version" --> "toybox 0.8.0-android"
"tail --help" --> "... -n ... -c ... -f ..."
"ls" --> "" // EMPTY
"ls /" --> "" // EMPTY
"ls /system/" --> "... whole bunch of directories including bin ..."
"ls /system/bin/" --> "" // EMPTY
(I figure I am getting no output (EMPTY) in some cases due to permissions/file ownership.)

I would really like to run "tail -f ..." but there are no events to get the output as it is appended to the file. No worries, I think I can use "tail -n ..." to get the last few lines, compare the result to previous result and see what changed. If I can do this fast enought, it should work, So this is my current working plan... but I am running into trouble specifying the correct file path. My file is located in AppInventor/Data, let's call in tailme.txt

I need to run " tail -n 4 [some path]/tailme.txt " but I can't figure out the path to AppInventor/data location. Any idea?

Try this path

/mnt/sdcard/tailme.txt

2 Likes

Note that when you are testing via the Companion, a dedicated Companion path is used. The APK path to be used is determined by you and I think it's a good idea to make a specific folder for the public files your App needs to process.

All of this is made easy by Taifun's file extension:
https://puravidaapps.com/file.php

There is probably a javascript lib that can process files in the same way as Linux, for example Tail.js written by adobe? App Inventor is good with javascript and there is in fact a web view extension that makes it even easier.

1 Like

This works for a file stored in the root of the sdcard:

image

I do not believe you can use follow, due to the constraints of the system and the extension, but you can use a clock timer to routinely check the tail. You can output the tail to a label.

2 Likes

Yay! /mnt/sdcard/... does the trick. I was using variations of /storage/emulated/... and also path indicated in file details info by file manager. It's a bit confusing since I can locate my files under "internal storage" and the "sd card" is labeled as "not inserted", yet the correct place is /mnt/sdcard/.

Thanks for all your help.

This path

/storage/emulated/0/text.txt

should also work:

1 Like

See also here:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.