Class FileUtils
-
- All Implemented Interfaces:
public class FileUtils
-
-
Field Summary
Fields Modifier and Type Field Description public final static intFILE_SCHEME_LENGTH
-
Method Summary
Modifier and Type Method Description static InputStreamgetFileInputStream(String path)Creates an input stream for reading the specified file into memory. static Array<byte>getByteFromUri(Uri uri)Reads a file as bytes, commonly used for binary files such as images, audio, and video. static voidwriteByte(Uri uri, Array<byte> data)Writes byte array sequentially to a file output stream. static FileconvertBitmap2File(Bitmap bm, String dir, String name)Converts a bitmap to a file. static FilecopyFile(File src, String path, String name)Copies a file. static booleancopyFile(String srcPath, String path, String name)Copies a file. static booleancopyFileToInternal(Context context, Uri srcUri, String desPath, String name)static booleancopyFile(Context context, Uri srcUri, String desPath)static StringgetFileNameWithPath(String path)Gets the file name from a file path. static Array<byte>file2byte(File file)Gets the byte array of the specified file. static Array<byte>file2byte(Context context, Uri uri)static Array<byte>contentFile2byte(Context context, Uri uri)Gets the byte array of the specified file. static Filebyte2File(Array<byte> buf, String filePath, String fileName)Creates a file from a byte array. static StringgetCachePath(Context context)Gets the cache storage path, e.g. static StringgetFileKey(Object message)Gets the key for a media message, used for image, video, GIF message handlers. static StringgetCachePath(Context context, String dir)Gets the cache storage path, e.g. static StringgetCacheDirsPath(Context context, String dir)Gets the cache storage path, e.g. static StringgetTempFilePath(Context context, int messageId)Gets the temporary file storage path for resumable downloads. static StringgetTempFilePath(Context context, String id)Gets the temporary file storage path for resumable downloads. static StringgetInternalCachePath(Context context, String dir)Gets the app internal cache path data/data/<package name>/cache/<dir>static StringgetMediaDownloadDir(Context context)Gets the media file storage directory. static StringgetMediaDownloadDir(Context context, String dir)Gets the media file storage directory. static longgetFileSize(File file)Gets the size of the specified file. static voidsaveFile(String str, String filePath)Saves a string to the specified file path. static StringgetStringFromFile(String path)Reads file content as a string. static voidremoveFile(String path)Deletes a file. static StringgetTempFileMD5(Context context, int messageId)Gets the MD5 key for resumable file download, used to identify the file being downloaded. static StringgetTempFileMD5(Context context, String tag)Gets the temporary file name for resumable download, used to identify the file being downloaded. static intreadPictureDegree(Context context, String path)Reads the rotation degree of an image. static booleanisFileExistsWithUri(Context pContext, Uri pUri)Checks whether a file exists. static longgetFileLengthWithUri(Context pContext, Uri pUri)Gets the file length. static booleanisValidateLocalUri(Uri pUri)Checks whether the file Uri scheme starts with "file" or "content". static booleanuriStartWithFile(Uri pUri)Checks whether the file Uri scheme starts with "file" and its length exceeds 7. static booleanuriStartWithContent(Uri srcUri)Checks whether the file Uri scheme starts with "content". static StringgetSuffix(Uri srcUri)Gets the file extension from a Uri. static StringgetSuffix(String path)Gets the file extension from a path. static FileInfogetFileInfoByUri(Context context, Uri uri)Gets file name, size, and type from a URI. static FileInfogetFileInfoByFile(File file)Gets file name, size, and type from a File object. static StringgetFileTypeFromInputStream(InputStream inputStream)Detects the file type from an input stream. static StringgetFileTypeFromByteData(Array<byte> data)static InputStreamgetFileInputStream(Context context, Uri uri)static FilegetExternalCacheDir(Context context)On versions below 9. static StringgenerateKey()static StringgetJsonOutPath(Context context)static StringsanitizeFilename(String displayName)Sanitizes the file name to prevent path traversal attacks. static StringgetUrlFileName(String url, String fileName)-
-
Method Detail
-
getFileInputStream
static InputStream getFileInputStream(String path)
Creates an input stream for reading the specified file into memory.
- Parameters:
path- file path- Returns:
input stream for the file
-
getByteFromUri
static Array<byte> getByteFromUri(Uri uri)
Reads a file as bytes, commonly used for binary files such as images, audio, and video.
- Parameters:
uri- file URI- Returns:
byte array read from the file
-
writeByte
static void writeByte(Uri uri, Array<byte> data)
Writes byte array sequentially to a file output stream.
- Parameters:
uri- file Uridata- byte array
-
convertBitmap2File
static File convertBitmap2File(Bitmap bm, String dir, String name)
Converts a bitmap to a file.
- Parameters:
bm- bitmapdir- storage directory pathname- file name- Returns:
the output file
-
copyFile
static File copyFile(File src, String path, String name)
Copies a file.
- Parameters:
src- source filepath- destination directory pathname- destination file name- Returns:
the destination file
-
copyFile
static boolean copyFile(String srcPath, String path, String name)
Copies a file. Only supports file:// scheme paths.
- Parameters:
srcPath- source file pathpath- destination directory pathname- destination file name- Returns:
whether the copy was successful
-
copyFileToInternal
static boolean copyFileToInternal(Context context, Uri srcUri, String desPath, String name)
- Parameters:
context- contextsrcUri- source URI starting with file:// or content://desPath- destination path under the app's package directory- Returns:
whether the file was copied successfully
-
copyFile
static boolean copyFile(Context context, Uri srcUri, String desPath)
- Parameters:
context- contextsrcUri- content:// URI obtained from MediaStoredesPath- destination path under the app's package directory- Returns:
whether the file was copied successfully
-
getFileNameWithPath
static String getFileNameWithPath(String path)
Gets the file name from a file path.
- Parameters:
path- file path- Returns:
file name
-
file2byte
static Array<byte> file2byte(File file)
Gets the byte array of the specified file.
- Parameters:
file- the file- Returns:
byte array of the file
-
contentFile2byte
static Array<byte> contentFile2byte(Context context, Uri uri)
Gets the byte array of the specified file. The URI must start with content://.
- Parameters:
context- contexturi- content:// URI- Returns:
byte array of the file
-
byte2File
static File byte2File(Array<byte> buf, String filePath, String fileName)
Creates a file from a byte array.
- Parameters:
buf- byte arrayfilePath- destination directory pathfileName- file name- Returns:
the created file
-
getCachePath
static String getCachePath(Context context)
Gets the cache storage path, e.g.
/sdcard/Android/data/<package name>/cache- Parameters:
context- Context- Returns:
full directory path, or empty string "" if external storage is not mounted or other error occurs
-
getFileKey
static String getFileKey(Object message)
Gets the key for a media message, used for image, video, GIF message handlers.
- Parameters:
message- message- Returns:
media message key for local storage
-
getCachePath
static String getCachePath(Context context, String dir)
Gets the cache storage path, e.g.
/sdcard/Android/data/<package name>/cache/<dir>- Parameters:
context- Contextdir- custom directory name- Returns:
full directory path, or empty string "" if external storage is not mounted or other error occurs
-
getCacheDirsPath
static String getCacheDirsPath(Context context, String dir)
Gets the cache storage path, e.g.
/sdcard/Android/data/<package name>/cache/<dir>- Parameters:
context- Contextdir- custom directory name (supports multi-level directories)- Returns:
full directory path, or empty string "" if external storage is not mounted or other error occurs
-
getTempFilePath
static String getTempFilePath(Context context, int messageId)
Gets the temporary file storage path for resumable downloads.
- Parameters:
context- contextmessageId- message ID- Returns:
storage path
-
getTempFilePath
static String getTempFilePath(Context context, String id)
Gets the temporary file storage path for resumable downloads.
- Parameters:
context- contextid- unique file identifier- Returns:
storage path
-
getInternalCachePath
static String getInternalCachePath(Context context, String dir)
Gets the app internal cache path
data/data/<package name>/cache/<dir>- Parameters:
context- Contextdir- custom directory name- Returns:
full directory path
-
getMediaDownloadDir
@Deprecated() static String getMediaDownloadDir(Context context)
Gets the media file storage directory.
- Parameters:
context- context- Returns:
media file storage path
-
getMediaDownloadDir
static String getMediaDownloadDir(Context context, String dir)
Gets the media file storage directory.
- Parameters:
context- contextdir- custom directory name- Returns:
media file storage path
-
getFileSize
static long getFileSize(File file)
Gets the size of the specified file.
- Parameters:
file- the file- Returns:
file length
-
saveFile
static void saveFile(String str, String filePath)
Saves a string to the specified file path.
- Parameters:
str- the string to savefilePath- destination file path
-
getStringFromFile
static String getStringFromFile(String path)
Reads file content as a string.
- Parameters:
path- file path- Returns:
file content as string
-
removeFile
static void removeFile(String path)
Deletes a file.
- Parameters:
path- file path
-
getTempFileMD5
static String getTempFileMD5(Context context, int messageId)
Gets the MD5 key for resumable file download, used to identify the file being downloaded.
- Parameters:
context- contextmessageId- message ID- Returns:
the key
-
getTempFileMD5
static String getTempFileMD5(Context context, String tag)
Gets the temporary file name for resumable download, used to identify the file being downloaded.
- Parameters:
context- contexttag- unique file identifier- Returns:
the key
-
readPictureDegree
static int readPictureDegree(Context context, String path)
Reads the rotation degree of an image.
- Parameters:
context- contextpath- absolute image path- Returns:
rotation degree
-
isFileExistsWithUri
static boolean isFileExistsWithUri(Context pContext, Uri pUri)
Checks whether a file exists.
- Parameters:
pContext- contextpUri- file Uri- Returns:
trueif the file exists,falseotherwise
-
getFileLengthWithUri
static long getFileLengthWithUri(Context pContext, Uri pUri)
Gets the file length. Returns -1 if the file does not exist.
- Parameters:
pContext- contextpUri- file Uri- Returns:
file length, or -1 if not found
-
isValidateLocalUri
static boolean isValidateLocalUri(Uri pUri)
Checks whether the file Uri scheme starts with "file" or "content".
- Parameters:
pUri- file Uri- Returns:
true if the Uri scheme starts with "file" or "content"
-
uriStartWithFile
static boolean uriStartWithFile(Uri pUri)
Checks whether the file Uri scheme starts with "file" and its length exceeds 7.
- Parameters:
pUri- file Uri- Returns:
true if the Uri scheme is "file" and its length exceeds 7
-
uriStartWithContent
static boolean uriStartWithContent(Uri srcUri)
Checks whether the file Uri scheme starts with "content".
- Parameters:
srcUri- file Uri- Returns:
true if the Uri scheme is "content"
-
getSuffix
static String getSuffix(Uri srcUri)
Gets the file extension from a Uri.
- Parameters:
srcUri- file Uri- Returns:
extension, may be null
-
getSuffix
static String getSuffix(String path)
Gets the file extension from a path.
- Parameters:
path- file path- Returns:
extension, may be null
-
getFileInfoByUri
static FileInfo getFileInfoByUri(Context context, Uri uri)
Gets file name, size, and type from a URI.
- Parameters:
context- contexturi- supports content:// and file:// schemes- Returns:
FileInfo containing file name, size, and type
-
getFileInfoByFile
static FileInfo getFileInfoByFile(File file)
Gets file name, size, and type from a File object.
- Parameters:
file- the file- Returns:
FileInfo containing file name, size, and type
-
getFileTypeFromInputStream
static String getFileTypeFromInputStream(InputStream inputStream)
Detects the file type from an input stream.
- Parameters:
inputStream- input stream- Returns:
file type extension
-
getFileTypeFromByteData
static String getFileTypeFromByteData(Array<byte> data)
-
getFileInputStream
static InputStream getFileInputStream(Context context, Uri uri)
-
getExternalCacheDir
static File getExternalCacheDir(Context context)
On versions below 9.0, getExternalCacheDir may throw ArrayIndexOutOfBoundsException
-
generateKey
static String generateKey()
-
getJsonOutPath
static String getJsonOutPath(Context context)
-
sanitizeFilename
static String sanitizeFilename(String displayName)
Sanitizes the file name to prevent path traversal attacks.
- Parameters:
displayName- file name- Returns:
sanitized file name
-
getUrlFileName
static String getUrlFileName(String url, String fileName)
-
-
-
-