public class FileSystemStorage extends Object
Unlike networking, the file system storage mostly tries to emulate java.io.File with
some simplifications for mobile devices.
Check out a more thorough discussion of this API here
.
A lot of API's rely on FileSystemStorage
as its the API native code usually uses consistently.
E.g. in this sample below the FileSystemStorage
is used to save a screenshot image for sharing
on social media:
The sample below shows the FileSystemStorage
as a tree:
Modifier and Type | Field and Description |
---|---|
static int |
ROOT_TYPE_MAINSTORAGE
Represents the type for the get root type method, this type generally represents the main
phone memory
|
static int |
ROOT_TYPE_SDCARD
Represents the type for the get root type method, this type generally represents an
SD card although due to variability in phone standards an SD card might be
detected incorrectly.
|
static int |
ROOT_TYPE_UNKNOWN
Returned for different types of root for which there is no specific knowledge one
way or the other.
|
Modifier and Type | Method and Description |
---|---|
void |
delete(String file)
Deletes the specific file or empty directory.
|
void |
deleteRetry(String file,
int retryCount)
Deletes the specific file or empty directory, if the platform supports a
delete on exit this method will activate it.
|
boolean |
exists(String file)
Indicates whether a file exists
|
String |
getAppHomePath()
The application home directory is a "safe place" to store files for this application in a portable way.
|
String |
getCachesDir()
Returns a device specific directory designed for cache style files, or null if
hasCachesDir()
is false |
char |
getFileSystemSeparator()
Returns the file system separator char normally '/'
|
static FileSystemStorage |
getInstance()
This class is a singleton
|
long |
getLastModified(String file)
Deprecated.
this API requires additional privacy permissions on iOS
and might cause problems with iOS submissions
|
long |
getLength(String file)
Returns the length of the file
|
long |
getRootAvailableSpace(String root)
Returns the available space in the given root directory
|
String[] |
getRoots()
Returns the filesystem roots from which the structure of the file system
can be traversed
|
long |
getRootSizeBytes(String root)
Returns the size of the given root directory
|
int |
getRootType(String root)
Returns the type of the root often by guessing
|
boolean |
hasCachesDir()
Returns true if the device has a directory dedicated for "cache" files
|
boolean |
isDirectory(String file)
Indicates whether the given file is a directory
|
boolean |
isHidden(String file)
Indicates the hidden state of the file
|
String[] |
listFiles(String directory)
Lists the files within the given directory, returns relative file names and not
full file names.
|
void |
mkdir(String directory)
Creates the given directory
|
InputStream |
openInputStream(String file)
Opens an input stream to the given file
|
OutputStream |
openOutputStream(String file)
Opens an output stream to the given file
|
OutputStream |
openOutputStream(String file,
int offset)
Opens an output stream to the given file
|
void |
rename(String file,
String newName)
Renames a file to the given name, expects the new name to be relative to the
current directory
|
void |
setHidden(String file,
boolean h)
Toggles the hidden state of the file
|
String |
toNativePath(String path)
Converts a file system path to a native path.
|
public static final int ROOT_TYPE_MAINSTORAGE
public static final int ROOT_TYPE_SDCARD
public static final int ROOT_TYPE_UNKNOWN
public static FileSystemStorage getInstance()
public String[] getRoots()
public int getRootType(String root)
root
- the root whose type we are checkingpublic String[] listFiles(String directory) throws IOException
directory
- the directory in which files should be listedIOException
public long getRootSizeBytes(String root)
root
- the root directory in the filesystempublic long getRootAvailableSpace(String root)
root
- the root directory in the filesystempublic void mkdir(String directory)
directory
- the directory name to createpublic void delete(String file)
file
- file or empty directory to deletepublic void deleteRetry(String file, int retryCount)
file
- file to deleteretryCount
- the number of times to retrypublic boolean exists(String file)
file
- the file to checkpublic boolean isHidden(String file)
file
- filepublic void setHidden(String file, boolean h)
file
- fileh
- hidden statepublic void rename(String file, String newName)
file
- absolute file namenewName
- relative new namepublic long getLength(String file)
file
- filepublic long getLastModified(String file)
public boolean isDirectory(String file)
file
- filepublic char getFileSystemSeparator()
public OutputStream openOutputStream(String file) throws IOException
file
- the fileIOException
public InputStream openInputStream(String file) throws IOException
file
- the fileIOException
public OutputStream openOutputStream(String file, int offset) throws IOException
file
- the fileoffset
- position in the fileIOException
public String getAppHomePath()
The application home directory is a "safe place" to store files for this application in a portable way.
On some platforms such as Android & iOS this path may be visible only to the
application itself, other apps won't have permission to access this path.
The sample below uses the app home directory to save a file so we can share it using the ShareButton
:
public boolean hasCachesDir()
public String getCachesDir()
hasCachesDir()
is false