public final class

FileStorage

extends Object
java.lang.Object
   ↳ com.neomades.io.file.FileStorage

Class Overview

Provides accesses to the various defined area of the filesystem.

It is necessary to understand how the filesystem is organized before interacting with it.
There are several places where data can be saved or read for an application:

  • the application cache (getCacheDir()): data the user does not need to access but that improve application performance if stored. This area may be clean by the filesystem if it requires space.
  • the application private space (getApplicationDir()): area in the application for user content (not limited in space). Data will only be available in the application.
  • an application private space but on the externral storage (getExternalApplicationDir()), only for Android: it can be the SDCard or a dedicated storage area on the device but it is a space reserved to the application. it cannot be accessed outside the application. For cross-platform compatibility, prefer using getApplicationDir().
  • outside application private space (#getExternalDir()), only for Android: it can be the SDCard or a dedicated storage area on the device but it is a space outside the application. This enables to access data outside the application. For cross-platform compatibility, prefer using getApplicationDir().

The EXTERNAL_STORAGE permission is needed to use the #getExternalDir() method on the Android platform.

Summary

Public Methods
static File getApplicationDir()
Returns the absolute path to a directory to store user-generated content inside the application reserved area.
static File getCacheDir()
Returns the absolute path to the application specific cache directory on the filesystem.
static File getExternalApplicationDir()
Returns the primary shared/external storage directory if possible.
static boolean isExternalDirAvailable()
Returns true if the external storage can be accessed in the filesystem.
static void requestExternalDir(ExternalDirCallback externalDirCallback)
Requests for the primary shared/external storage directory.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public static File getApplicationDir ()

Returns the absolute path to a directory to store user-generated content inside the application reserved area.

This folder should be use to store any contents that must be available for the user (to create, import, delete or edit). For example, for a drawing image it can be any graphic files the user creates, for an audio player it can be downloaded music...

In all platforms this folder is back up with the application.

No permissions are required to work with the application directory.

Returns
  • the path of the directory holding application files (never null)

public static File getCacheDir ()

Returns the absolute path to the application specific cache directory on the filesystem.

The system may automatically delete files in this directory as disk space is needed elsewhere on the device.

This folder must be used to save some data that are not mandatory for the application but that can improve performance. For example, it can be database cache files and transient, downloadable content...

Try to keep the cache to a reasonable space (around 1MB) since the filesystems will prefer to delete big cache first if it requires space.

No permissions are required to work with the cache directory.

Returns
  • the root cache directory (never null)

public static File getExternalApplicationDir ()

Returns the primary shared/external storage directory if possible. Otherwise, returns the application reserved area (Application dir).
These files are considered as internal to the applications even if they are in the external storage (it is the main difference with #getExternalDir(). Typically, data will not be visible to the user as media. This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened.

By external storage, it can be the SDCard or a dedicated storage area on the device. It depends of the implementation of the functionality on the device.

No permissions are required to work with the cache directory.

Cross-Platform Limitations

Available only on Android In other platforms, this method returns the internal application dir (the same path as getApplicationDir()).

Returns
  • the absolute path to application-specific directory (SDCard or Internal Memory)
See Also

public static boolean isExternalDirAvailable ()

Returns true if the external storage can be accessed in the filesystem. It is true only on Android.

Returns
  • true if the directory is available

public static void requestExternalDir (ExternalDirCallback externalDirCallback)

Requests for the primary shared/external storage directory. This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened.

By external storage, it can be the SDCard or a dedicated storage area on the device. It depends of the implementation of the functionality on the device.

Contrary to getApplicationDir() this does not write in a protected area within the application. The data will be accessible outside the application. Prefer using getApplicationDir() except if you want the data to be accessible outside the application.

The EXTERNAL_STORAGE permission is needed to use this method.

To write data on the external storage but on a place reserved to the application, use getExternalApplicationDir().

Cross-Platform Limitations

Available only on Android

Parameters
externalDirCallback a callback to retrieve the directory and information around it (availability on the platform, user permissions...)