FileTools : Some tools to work with files

Hi!
How do I dynamically get the root path of the directory where my app is installed?

In the Internal Storage, the app package is stored, which can only be accessed with a rooted device.
/data/data/<packageName>/

What does “dynamically” mean?

1 Like

I need to access a File.pdf with Start Activity. On my android 8.1 device I used the URI. file: ///sdcard/Makeroid/library/File.pdf. But on my other android 4.4 device it only worked with the URI. file: ///storage/Makeroid/library/File.pdf.
according to the device the uri will be started with sdcard or storage how can i identify this automatically?

you could use

  1. a relative path, which is /library/File.pdf or
  2. a full generic path, which is file:///mnt/sdcard/library/File.pdf

EDIT: are you trying to read a file form the assets of the app? this will not work like this…

Taifun

3 Likes

The root directory of the External Storage is:

file:///mnt/sdcard/ or
file:///storage/emulated/0/ 
/storage/emulated/0/.

file:///sdcard/Makeroid/library/
is the same as:
file:///storage/emulated/0/Makeroid/library/ or
/storage/emulated/0/Makeroid/library/

These dirs are visible / accessible on your device, where (from an Android point of view) this “External Storage” is called “Internal Storage” (see below):
Internal Storage > Makeroid > library

For a webView the path to the assets is for the compiled app (APK):
file:///android_asset/
and for Companion:
file:///storage/emulated/0/Makeroid/assets/

FOR CLARIFICATION:
From an Android point of view there are an Internal Storage and an External Storage.

1. Internal Storage
In the Internal Storage, the app package is saved:

/data/data/<packageName>/

which can only be accessed with a rooted device.

2. External Storage
The root directory of the External Storage is:

/storage/emulated/0/

2.1. App-specific directory
In addition, there may be an app-specific directory (which can be created with Taifun’s File extension: App Inventor Extensions: File | Pura Vida Apps or the FileTools extension)

 /storage/emulated/0/Android/data/<packageName>/files/

which is saved in the External (private) Storage, but does not require READ_ / WRITE_EXTERNAL_STORAGE permissions.
This app-specific (private) folder can only be accessed by your app.

See: Manifest.permission  |  Android Developers

2.2. Removable (micro) SD card
There may also be another External Storage: a removable (micro) SD card, eg:

/storage/82C3-E96C/

that can only be read (on modern Android systems, since Android KitCat).

Note:
The root directory of the External Storage

/storage/emulated/0/

is displayed on the device as Internal Storage (unfortunately this is a bit ambiguous).

10 Likes

Great! I’ve learned a lot about this.
But I wonder if there is a way to find out if the APK has been installed on internal or external storage. Is there this possibility?

As I said: /data/data/<packageName>/ (see above: 1. Internal Storage)

See here for an excellent overview on this topic: https://www.dev2qa.com/android-read-write-external-storage-file-example/

"Internal storage is used to store android app private data such as files, cached files, shared_preferences and database files.

All those files is stored in android device folder /data/data/<app packageName>/ . And only the android app can operate those files. In general internal storage files is saved in android device build-in flash disk memory."

It worked! It ran on two devices (the apk installed one on internal sdcard and one on external storage) using only the path: file: /// storage / emulated / 0 / Android / data / packname / files.
Thanks for listening! \The/

Copying from the assets does not work for APK (checked with AI2):

Two slashes from file:///android_asset/ are removed, result:
Runtime Error: file:/android_asset/test.1.pdf (No such file or directory)

usually (and this is how the file component is implemented) to access the assets you have to use two slashes //, see also the documentation Storage

Prefix the filename with / to read from a specific file on the SD card. for instance /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).

in case this extension uses another logic, it would not be very helpful and would confuse everybody…

btw. the path file:///android_asset/ is only relevant for the webviewer to access files stored in the assets…

Taifun

2 Likes

Of course, I have tried this first: //test1.pdf
Doesn’t work:
Runtime Error: /test.1.pdf (No such file or directory)

1 Like

Thanks @bodymindpower :heart_eyes:
I hope next version will solve this and other issues/bugs :wink:

3 Likes

Video on how to use…copy from SD card to phone or vise versa

ChangeLog Version4

New blocks
Added 6 new blocks:

Changes

  • Replaced path with name.Now it will find path automatically from file name (Thanks to App Inventor)
  • Some other minor changes

Bug Fixes

  • Now it is able to copy files from assets (Thanks @bodymindpower)
  • ‘AvailableStorageDirectories’ block is working now (Thanks @salem_m_s2021)
7 Likes

Is there more than thank you

1 Like

I have no idea about that :upside_down_face:

1 Like

We need an extension of that :crazy_face: :joy:

1 Like

There is already an extension :innocent:

1 Like

ChangeLog Version5

Changes

  • Added ‘recursive’ in FilesList and FileListAsync

image

image

Bug Fixes

  • Fixed bug in Delete method
  • Fixed bug in getting assets list using FilesList.Use // in FilesList as dir to get Assets list while using Companion
  • Some minor bug fixes
6 Likes

Due to this extension my automatically my mobile’s all images and videos and other files lost when i export my app and install it, I dont used this extension in blocks remains unchanged, get file path only this block is used… and after installing all data lost… if it is a bug then solve it… if it is hacked then dont use anyone…

1 Like