Package com.unitech.api.file
Class FileCtrl
java.lang.Object
com.unitech.api.file.FileCtrl
This controller is used to read / write file to storage with system permission.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionBundleTests whether the application can read the file denoted by this abstract pathname
not support canRead for path start with /Android/data or /Android/obbBundleTests whether the application can modify the file denoted by this abstract pathname.
not support canWrite for path start with /Android/data or /Android/obbBundlecloseFileStream(long fileStreamId) Close the specific file stream that created by FileCtrl.createFileStream.BundleCopy file bundle.BundlecreateFile(String path) Create file or folder on deviceBundlecreateFileStream(String filepath, boolean keepContents) Create a stream that you can use the stream to read or write data into the file.
If you have completed your operation on this stream, you should call FileCtrl.closeFileStream(fileStreamId) to close this stream.BundledeleteFile(String path) Deletes the file or folder.BundleCheck if the file or folder exists.Bundleget device external storage information include real path and descriptionstatic FileCtrlgetInstance(Context context) Gets instance.BundleisDirectory(String path) Check if path is directory.BundleReturns an array of strings naming the files and directories in the
directory denoted by this abstract pathname.Bundleopen file for the given package name and path
not support openFile for path start with /Android/data or /Android/obbBundlepathConverter(String path) get real storage path descriptionBundlereadFromFile(String path) Read from file.BundleRenames the file denoted by this name.
not support for path start with /Android/data or /Android/obbBundlewriteToFile(String path, byte[] content) Write to file with byte[]BundlewriteUTF8ToFile(String path, String content) Write String(utf8) to file.
-
Field Details
-
BUNDLE_ERROR_CODE
- See Also:
-
BUNDLE_ERROR_MSG
- See Also:
-
BUNDLE_DATA
- See Also:
-
BUNDLE_PARCEL_FILE_DESCRIPTOR
- See Also:
-
BUNDLE_FILE_STREAM_ID
- See Also:
-
RESULT_CODE_ERROR
public static final int RESULT_CODE_ERROR- See Also:
-
RESULT_CODE_SUCCESS
public static final int RESULT_CODE_SUCCESS- See Also:
-
-
Method Details
-
getInstance
Gets instance.- Parameters:
context- the context- Returns:
- the instance
-
writeUTF8ToFile
Write String(utf8) to file.- Parameters:
path- the path (String)content- the content (String)- Returns:
- A bundle contains the error code and message
int errorCode = bundle.getInt("errorCode"), (error:1, success=0)
String errorMsg = bundle.getString("errorMsg");
-
writeToFile
Write to file with byte[]- Parameters:
path- the path (String)content- the content (byte[], but if the input source only allows strings, you can use base64 to encode the byte[] into a string to process byte input, e.g. SXQncyBhIGJlYXV0aWZ1bCBkYXkgdG9kYXku)- Returns:
- A bundle contains the error code and message
int errorCode = bundle.getInt("errorCode"), (error:1, success=0)
String errorMsg = bundle.getString("errorMsg");
-
readFromFile
Read from file.- Parameters:
path- the path (String)- Returns:
- A bundle contains the error code and message
int errorCode = bundle.getInt("errorCode"), (error:1, success=0)
String errorMsg = bundle.getString("errorMsg");
byte[] content = bundle.getByteArray("Data");
String base64Content = bundle.getString("Base64Data");
String utf8Content = bundle.getString("UTF8Data") if there is
-
copyFile
Copy file bundle.- Parameters:
fromPath- the from path (String) e.g. /sdcard/test.txttoPath- the to path (String) e.g. /sdcard/Download/test.txt- Returns:
- A bundle contains the error code and message
int errorCode = bundle.getInt("errorCode"), (error:1, success=0)
String errorMsg = bundle.getString("errorMsg");
-
createFile
Create file or folder on device- Parameters:
path- The path of file or folder. If you want to create folder, the path should end with "/" like "/sdcard/newfolder/" (String)- Returns:
- A bundle contains the error code and message
int errorCode = bundle.getInt("errorCode"), (error:1, success=0)
String errorMsg = bundle.getString("errorMsg");
-
deleteFile
Deletes the file or folder. If this pathname denotes a folder, then the folder must be empty in order to be deleted.
cannot delete folder within /Android/data or /Android/obb- Parameters:
path- The path of file or folder. (String)- Returns:
- A bundle contains the error code and message
int errorCode = bundle.getInt("errorCode"), (error:1, success=0)
String errorMsg = bundle.getString("errorMsg");
-
createFileStream
Create a stream that you can use the stream to read or write data into the file.
If you have completed your operation on this stream, you should call FileCtrl.closeFileStream(fileStreamId) to close this stream.- Parameters:
filepath- The file path that you want to make a stream on it. (String)keepContents- Whether you want to keep the file contents. If you want to read data from the stream, you should set keepContents as true. (true: keep, false: erase file) (boolean)- Returns:
- A bundle contains the error code and message
int errorCode = bundle.getInt("errorCode"), (error:1, success=0)
String errorMsg = bundle.getString("errorMsg");
ParcelFileDescriptor pfd = bundle.getParcelable(FileCtrl.BUNDLE_PARCEL_FILE_DESCRIPTOR);
long fileStreamId = bundle.getLong(FileCtrl.BUNDLE_FILE_STREAM_ID);
-
closeFileStream
public Bundle closeFileStream(long fileStreamId) Close the specific file stream that created by FileCtrl.createFileStream.- Parameters:
fileStreamId- The file stream ID that you want to close. (long)- Returns:
- A bundle contains the error code and message
int errorCode = bundle.getInt("errorCode"), (error:1, success=0)
String errorMsg = bundle.getString("errorMsg");
-
exists
Check if the file or folder exists.- Parameters:
path- The path of the file or folder. (String)- Returns:
- A bundle contains the error code and message
int errorCode = bundle.getInt("errorCode"), (1: file or folder does not exist, 0: file or folder exists)
String errorMsg = bundle.getString("errorMsg");
-
isDirectory
Check if path is directory.- Parameters:
path- The path of the file or folder. (String)- Returns:
- A bundle contains the error code and message
int errorCode = bundle.getInt("errorCode"), (1: path is not directory, 0: path is directory)
String errorMsg = bundle.getString("errorMsg");
-
openFile
open file for the given package name and path
not support openFile for path start with /Android/data or /Android/obb- Parameters:
path- The path of the file. (String)- Returns:
- A bundle contains the error code and message
int errorCode = bundle.getInt("errorCode"), (1: Cannot open the file, 0: success to open the file)
String errorMsg = bundle.getString("errorMsg");
-
list
Returns an array of strings naming the files and directories in the
directory denoted by this abstract pathname.
not support for path start with /Android/data or /Android/obb- Parameters:
path- The path of the file or folder. (String)- Returns:
- A bundle contains the error code and message
int errorCode = bundle.getInt("errorCode"), (1: list is null, 0: list is not null)
String errorMsg = bundle.getString("errorMsg");
-
renameTo
Renames the file denoted by this name.
not support for path start with /Android/data or /Android/obb- Parameters:
path- The path of the file or folder. (String)- Returns:
- A bundle contains the error code and message
int errorCode = bundle.getInt("errorCode"), (1: fail, 0: success)
String errorMsg = bundle.getString("errorMsg");
-
canWrite
Tests whether the application can modify the file denoted by this abstract pathname.
not support canWrite for path start with /Android/data or /Android/obb- Parameters:
path- The path of the file or folder. (String)- Returns:
- A bundle contains the error code and message
int errorCode = bundle.getInt("errorCode"), (1: fail, 0: success)
String errorMsg = bundle.getString("errorMsg");
-
canRead
Tests whether the application can read the file denoted by this abstract pathname
not support canRead for path start with /Android/data or /Android/obb- Parameters:
path- The path of the file or folder. (String)- Returns:
- A bundle contains the error code and message
int errorCode = bundle.getInt("errorCode"), (1: fail, 0: success)
String errorMsg = bundle.getString("errorMsg");
-
getExternalStorageInfo
public Bundle getExternalStorageInfo()get device external storage information include real path and description- Returns:
- A bundle contains the error code and message
int errorCode = bundle.getInt("errorCode"), (1: fail, 0: success)
String errorMsg = bundle.getString("errorMsg");
-
pathConverter
get real storage path description- Parameters:
path- The path of the file or folder that start with USB/, SD/ or FLASH/ (String)- Returns:
- String indicate the real storage path
use bundle.getString("REAL_PATH") to get converted path int errorCode = bundle.getInt("errorCode"), (1: fail, 0: success)
String errorMsg = bundle.getString("errorMsg");
-