Files And Android Storage

Published on February 6, 2021

🏷️ Tags: #tutorials-and-guides

A. Introduction To Android Storage

For Android phones, saved files are sorted in two categories - internet and external storage.

Internal storage is the space available in a device to keep system files, applications, and user files. For example, the app package is saved in the internal storage of the device, in /data/data/<packageName>/. The private directory of your app in the internal storage is /data/user/0/<packageName>/files/, which can be used with the App Inventor File component to access and save files. Only your app, not even the system file management app, can access this directory, and when the user uninstalles the application, the directory is forever removed.

The MIT App Inventor AI Companion also has its own directory. When you import files into the Media and connects your Android device with your computer, App Inventor saves the files in your phone's internal storage. For users with Android ≥ 10 (SDK ≥ 29) devices, use /storage/emulated/0/Android/data/edu.mit.appinventor.aicompanion3/files/assets/, else if your phone is Android < 10, use /storage/emulated/0/Android/data/edu.mit.appinventor.aicompanion3/files/AppInventor/assets/.

External storage is one that is accessible by the user but still part of the built-in memory. To access the external storage, your device requires permissions to read and write the external storage. The root directory of the external storage can be /storage/emulated/0/, /sdcard/, file:///mnt/sdcard/ or file:///storage/emulated/0/.

Your app also has an ASD (Application Specific Directory) in the external storage. For users with Android > 9, the ASD is automatically created once you install the app, otherwise, use Taifun File to create and return the ASD path. The ASD can only be accessed by your app and is automatically removed when the app is uninstalled.

The path of the ASD is /storage/emulated/0/Android/data/<packageName>/files/, which although is in the external storage, it does not require the write permission. Check out Android Developers for more info.

Reading Files in App Inventor

The App Inventor system has a built-in File component for reading and writing files. However, before that, you have to specify the Scope property, which is where the files are placed.

  • App: the file is saved in the ASD.

  • Asset: the file is in the Media panel, which is the Assets. If a file name starts with two slashes (//), it will be counted as an asset file. Do not attempt to save a file in the Assets.

  • Cache: the file is saved in the cache directory of the application.

  • Legacy: the file will be read from and written to the file system using the App Inventor rules prior to release nb187. That is, file names starting with a single / will be read from and written to the root of the external storage directory, e.g., /sdcard/. Legacy functionality will not work on Android 11 or later.

  • Private: the file will be read and saved in the app's private directory.

  • Shared: the file will be read from and written to the device’s shared media directories, such as Pictures.

I have saved a file in Media as Device.txt, so this is how I can read the file and show it on a label.

If I want to save a file in the ASD, I can use this.

For more information, read the topic Some basics on Android storage. Thank you!