public class FileServices extends ApplicationContextProvider
If the application uses Google Play expansion files, then this class will attempt to fetch asset files within these expansion files first before looking for them within the APK's "assets" directory. By default, this class assumes that these expansion files are named after the application's package name and version code. If they use a different name (such as a different version code), then you can change it by calling this class' setMainExpansionFileName() and setPatchExpansionFileName() methods. If this application uses Corona's built-in Google Licensing feature, then Corona will set these file names automatically according to the names provided by the Google Licensing server.
All methods in this class are thread safe and can be called from any thread.
Constructor and Description |
---|
FileServices(android.content.Context context)
Creates an object that provides easy access to the file system and this application's
internal files in the APK's asset files and Google Play expansion files.
|
Modifier and Type | Method and Description |
---|---|
boolean |
copyFile(File sourceFile,
File destinationFile)
Copies the given file to the given destination safely without exceptions.
|
boolean |
copyFile(String sourceFilePath,
String destinationFilePath)
Copies the given file to the given destination safely without exceptions.
|
boolean |
doesAssetFileExist(String filePath)
Determines if the given file exists in the application's assets directory or in its expansion files.
|
boolean |
doesResourceFileExist(String filePath)
Determines if the given file exists in the application's resource directory
|
File |
extractAssetFile(File assetFile)
Extracts the given asset file to an external directory to make it available to native C/C++ APIs
that do not have access to this application's assets directory within the APK or its expansion files.
|
File |
extractAssetFile(File assetFile,
boolean overwrite)
Extracts the given asset file to an external directory to make it available to native C/C++ APIs
that do not have access to this application's assets directory within the APK or its expansion files.
|
File |
extractAssetFile(String filePath)
Extracts the given asset file to an external directory to make it available to native C/C++ APIs
that do not have access to this application's assets directory within the APK or its expansion files.
|
File |
extractAssetFile(String filePath,
boolean overwrite)
Extracts the given asset file to an external directory to make it available to native C/C++ APIs
that do not have access to this application's assets directory within the APK or its expansion files.
|
AssetFileLocationInfo |
getAssetFileLocation(String filePath)
Determines where the given asset file can be found.
|
byte[] |
getBytesFromFile(File file)
Fetches all bytes from the given file.
|
byte[] |
getBytesFromFile(String filePath)
Fetches all bytes from the given file.
|
File |
getExpansionFileDirectory()
Gets the directory where this application's "main" and "patch" expansion files are supposed to reside in.
|
String |
getExtensionFrom(File file)
Extracts the extension from the given file.
|
String |
getExtensionFrom(String fileName)
Extracts the extension from the given file name.
|
File |
getMainExpansionFile()
Gets the path and file name of this application's "main" expansion file.
|
String |
getMimeTypeFrom(android.net.Uri uri)
Handles requests for the MIME type of the data at the given URI.
|
File |
getPatchExpansionFile()
Gets the path and file name of this application's "patch" expansion file.
|
boolean |
isAssetFile(String filePath)
Determines if the given path\file name is an asset file inside of the APK or expansion file.
|
void |
loadExpansionFiles()
Loads this application's expansion files for fast access.
|
boolean |
moveFile(File sourceFile,
File destinationFile)
Moves the given file to the given destination safely without exceptions.
|
android.content.res.AssetFileDescriptor |
openAssetFileDescriptorFor(File file)
Safely opens a file via an AssetFileDescriptor without exceptions.
|
android.content.res.AssetFileDescriptor |
openAssetFileDescriptorFor(String filePath)
Safely opens a file via an AssetFileDescriptor without exceptions.
|
InputStream |
openFile(File file)
Safely opens an input stream to the given file without exceptions.
|
InputStream |
openFile(String filePath)
Safely opens an input stream to the given file without exceptions.
|
void |
setMainExpansionFileName(String fileName)
Sets the name of the "main" expansion file to be loaded.
|
void |
setPatchExpansionFileName(String fileName)
Sets the name of the "patch" expansion file to be loaded.
|
boolean |
writeToFile(InputStream inputStream,
File destinationFile)
Writes the given input stream's bytes to the destination file safely without exceptions.
|
getApplicationContext
public FileServices(android.content.Context context)
context
- Reference to an Android created context used to access the system's directories.
Setting this to null will cause an exception to be thrown.
public boolean isAssetFile(String filePath)
fileName
- The path and file name to check if it is an asset file. Cannot be null or empty.Returns false if the given file is an external file, null, or an empty string.
public boolean doesAssetFileExist(String filePath)
filePath
- Relative path to the asset file.Returns false if the file could not be found.
public boolean doesResourceFileExist(String filePath)
filePath
- Relative path to the resource file (e.g. "drawable/image.png").Returns false if the file could not be found.
public AssetFileLocationInfo getAssetFileLocation(String filePath)
Note that assets files are packaged within an APK file or within a Google Play expansion file. This method determines which package file contains the given asset, where the asset's bytes can be found within the package file, determines the asset's zip entry name if applicable, and whether or not the asset is compressed.
This information can be used to directly read the asset's bytes for fast access, provided that the asset is not compressed. This is particularly usefull if the asset needs to be accessed from native C/C++ code or from a 3rd party library that does not support Android's AssetManager class.
filePath
- Relative path to the assset file.Returns null if the asset could not be found or if given an invalid argument.
public File getExpansionFileDirectory()
Returns null if the expansion directory could not be found.
public void setMainExpansionFileName(String fileName)
This method is expected to be called if Google Licensing returns an expansion file name. Otherwise, the system will assume the expansion file name matches this application's package name and version code.
fileName
- The name of the "main" expansion file, including the extension, but excluding the path.
For example: "main.1.my.package.name.obb"
Set to null or empty string to use the default expansion file name using this application's package name and version code.
public File getMainExpansionFile()
Returns null if the expansion directory could not be found.
public void setPatchExpansionFileName(String fileName)
This method is expected to be called if Google Licensing returns an expansion file name. Otherwise, the system will assume the expansion file name matches this application's package name and version code.
fileName
- The name of the "patch" expansion file, including the extension, but excluding the path.
For example: "patch.1.my.package.name.obb"
Set to null or empty string to use the default expansion file name using this application's package name and version code.
public File getPatchExpansionFile()
Returns null if the expansion directory could not be found.
public void loadExpansionFiles()
Calling this method twice will cause it to close access to the last loaded expansion files and then reload the current expansion files. This method is called by the CoronaActivity's onCreate() method in case the existing expansion files were replaced while the application process was running in the background, which typically happens during testing.
public InputStream openFile(String filePath)
filePath
- The path and name of the file to be opened.Returns null if unable to find or access the given file.
public InputStream openFile(File file)
filePath
- The file to be opened.Returns null if unable to find or access the given file.
public android.content.res.AssetFileDescriptor openAssetFileDescriptorFor(String filePath)
filePath
- The path and name of the file to access.Returns null if the file could not be found.
public android.content.res.AssetFileDescriptor openAssetFileDescriptorFor(File file)
file
- The file to access.Returns null if the file could not be found.
public File extractAssetFile(String filePath)
If the given file has already been extracted, then this method will not attempt to extract it again unless this application has been re-installed on the device. For the re-installed case, Corona must assume that this application might have modified assets that need to be re-extracted.
filePath
- Relative path to the asset file within the APK's assets directory or expansion files.Returns null if unable to find the given asset file.
public File extractAssetFile(String filePath, boolean overwrite)
If the given file has already been extracted, you can tell force the rewrite
filePath
- Relative path to the asset file within the APK's assets directory or expansion files.overwrite
- Whether the file should be overwrite whats already extracted, if anything. True to overwrite.Returns null if unable to find the given asset file.
public File extractAssetFile(File assetFile)
If the given file has already been extracted, then this method will not attempt to extract it again unless this application has been re-installed on the device. For the re-installed case, Corona must assume that this application might have modified assets that need to be re-extracted.
assetFile
- The asset file within the APK's assets directory or expansion files.Returns null if unable to find the given asset file.
public File extractAssetFile(File assetFile, boolean overwrite)
If the given file has already been extracted, then this method WILL attempt to extract it again
assetFile
- The asset file within the APK's assets directory or expansion files.overwrite
- Whether the file should be overwrite whats already extracted, if anything. True to overwrite.Returns null if unable to find the given asset file.
public String getExtensionFrom(File file)
file
- The file to extract the extension from.Returns an empty string if the given file name does not have an extension.
Returns null if the given argument was null.
public String getExtensionFrom(String fileName)
fileName
- The path and name of the file to extract the extension from.Returns an empty string if the given file name does not have an extension.
Returns null if the given argument was null or an empty string.
public boolean copyFile(String sourceFilePath, String destinationFilePath)
sourceFilePath
- The path and file name to be copied. Cannot be null.destinationFilePath
- The path and file name to copy the source file to. Cannot be null.public boolean copyFile(File sourceFile, File destinationFile)
Will overwrite the destination file if it already exists.
Will create the destination's directories if it does not already exist.
sourceFile
- The file to be copied. Cannot be null.destinationFile
- The path and file name to copy the source file to. Cannot be null.public boolean writeToFile(InputStream inputStream, File destinationFile)
Will overwrite the destination file if it already exists.
Will create the destination's directories if it does not already exist.
inputStream
- The input stream to copy bytes from. Cannot be null.destinationFile
- The path and file name to copy the input stream's bytes to. Cannot be null.public boolean moveFile(File sourceFile, File destinationFile)
Will overwrite the destination file if it already exists.
Will create the destination's directories if it does not already exist.
sourceFile
- The file to be moved. Cannot be null.destinationFile
- The path and file name to move the file to. Cannot be null.
Does not have to have the same file name as the source.public byte[] getBytesFromFile(String filePath)
filePath
- The path and name of the file to read from.
Relative paths are assumed to be asset files.
Returns null if the file was not found or if the application does not have read permission.
public byte[] getBytesFromFile(File file)
file
- The file to read from.
If the file has a relative path, then it is assumed to be an asset file.
Returns null if the file was not found or if the application does not have read permission.
public String getMimeTypeFrom(android.net.Uri uri)
uri
- The URI to query.