Class FileUtils
-
- All Implemented Interfaces:
public class FileUtils
-
-
Field Summary
Fields Modifier and Type Field Description public final static int
FILE_SCHEME_LENGTH
-
Method Summary
Modifier and Type Method Description static InputStream
getFileInputStream(String path)
Creates an input stream pointing to the file, reading its contents into memory. static Array<byte>
getByteFromUri(Uri uri)
Reads a file in bytes, commonly used for reading binary files such as images, audio, video, etc. static void
writeByte(Uri uri, Array<byte> data)
Writes the byte array sequentially to the file output stream. static File
convertBitmap2File(Bitmap bm, String dir, String name)
Convert image to file static File
copyFile(File src, String path, String name)
Copy a file static boolean
copyFile(String srcPath, String path, String name)
Copy files, only supports files starting with 'file' static boolean
copyFileToInternal(Context context, Uri srcUri, String desPath, String name)
static boolean
copyFile(Context context, Uri srcUri, String desPath)
static String
getFileNameWithPath(String path)
Retrieves the file name based on the file path static Array<byte>
file2byte(File file)
Retrieves the byte array of the specified file. static Array<byte>
file2byte(Context context, Uri uri)
static Array<byte>
contentFile2byte(Context context, Uri uri)
Retrieves the byte array of the specified file, which must start with content. static File
byte2File(Array<byte> buf, String filePath, String fileName)
Generates a file based on the byte array static String
getCachePath(Context context)
Retrieves the cache storage path, such as /sdcard/Android/data/<package name>/cache
static String
getFileKey(Message message)
Retrieves the key for media messages, used for handling messages such as images, videos, and GIFs. static String
getCachePath(Context context, String dir)
Retrieves the cache storage path, such as /sdcard/Android/data/<package name>/cache/<dir>
static String
getCacheDirsPath(Context context, String dir)
Retrieves the cache storage path, such as /sdcard/Android/data/<package name>/cache/<dir>
static String
getTempFilePath(Context context, int messageId)
Get the temporary file storage path for resumable upload static String
getTempFilePath(Context context, String id)
Get the temporary file storage path for resumable uploads static String
getInternalCachePath(Context context, String dir)
Get the app cache storage path data/data/<package name>/cache/<dir>
static String
getMediaDownloadDir(Context context)
Get the storage path of media files static String
getMediaDownloadDir(Context context, String dir)
Get the storage path for media files static long
getFileSize(File file)
Get the size of the specified file static void
saveFile(String str, String filePath)
Stores the string at the specified path static String
getStringFromFile(String path)
Get file data static void
removeFile(String path)
Delete File static String
getTempFileMD5(Context context, int messageId)
Retrieves the MD5 key for file resumption, used to identify the file being downloaded. static String
getTempFileMD5(Context context, String tag)
Retrieves the temporary filename for resumable file download, used to identify the file being downloaded. static int
readPictureDegree(Context context, String path)
Reads the rotation angle of an image static boolean
isFileExistsWithUri(Context pContext, Uri pUri)
Checks if the file exists. static long
getFileLengthWithUri(Context pContext, Uri pUri)
Gets the file length. static boolean
isValidateLocalUri(Uri pUri)
Checks if the file URI scheme starts with file or content. static boolean
uriStartWithFile(Uri pUri)
Checks if the file Uri scheme starts with file and if the file Uri length exceeds 7 static boolean
uriStartWithContent(Uri srcUri)
Checks whether the file URI scheme starts with content. static String
getSuffix(Uri srcUri)
Get the suffix of the Uri static String
getSuffix(String path)
Get the URI suffix static FileInfo
getFileInfoByUri(Context context, Uri uri)
Retrieves the file name, size, and type from the URI. static FileInfo
getFileInfoByFile(File file)
Retrieves the file name, size, and type from the file static String
getFileTypeFromInputStream(InputStream inputStream)
Retrieves the file type from the input stream static String
getFileTypeFromByteData(Array<byte> data)
static InputStream
getFileInputStream(Context context, Uri uri)
static File
getExternalCacheDir(Context context)
// For versions below 9. static String
generateKey()
static String
getJsonOutPath(Context context)
static String
sanitizeFilename(String displayName)
Cleans the file name to prevent path traversal attacks static String
getUrlFileName(String url, String fileName)
-
-
Method Detail
-
getFileInputStream
static InputStream getFileInputStream(String path)
Creates an input stream pointing to the file, reading its contents into memory.
- Parameters:
path
- The file path.- Returns:
An input stream pointing to the file, reading its contents into memory.
-
getByteFromUri
static Array<byte> getByteFromUri(Uri uri)
Reads a file in bytes, commonly used for reading binary files such as images, audio, video, etc.
- Parameters:
uri
- File address- Returns:
Byte stream read from the file
-
writeByte
static void writeByte(Uri uri, Array<byte> data)
Writes the byte array sequentially to the file output stream.
- Parameters:
uri
- The file URIdata
- The byte array
-
convertBitmap2File
static File convertBitmap2File(Bitmap bm, String dir, String name)
Convert image to file
- Parameters:
bm
- The imagedir
- The storage path for the imagename
- The image name- Returns:
The file
-
copyFile
static File copyFile(File src, String path, String name)
Copy a file
- Parameters:
src
- Source filepath
- Destination file pathname
- Destination file name- Returns:
Destination file
-
copyFile
static boolean copyFile(String srcPath, String path, String name)
Copy files, only supports files starting with 'file'
- Parameters:
srcPath
- Source file pathpath
- Destination file pathname
- Destination file name- Returns:
Indicates whether the copy operation was successful
-
copyFileToInternal
static boolean copyFileToInternal(Context context, Uri srcUri, String desPath, String name)
- Parameters:
context
- The contextsrcUri
- Starts with file or contentdesPath
- The storage path under the package name, starting with storage:// (must be a directly accessible path under the package name)- Returns:
The file copied to the package name directory
-
copyFile
static boolean copyFile(Context context, Uri srcUri, String desPath)
- Parameters:
context
- ContextsrcUri
- URI obtained from MediaStore, starting with content://desPath
- Storage path under the package name, starting with storage:// (must be a directly accessible path under the package name)- Returns:
The file copied to the package-specific storage path
-
getFileNameWithPath
static String getFileNameWithPath(String path)
Retrieves the file name based on the file path
- Parameters:
path
- The file path- Returns:
The file name
-
file2byte
static Array<byte> file2byte(File file)
Retrieves the byte array of the specified file.
- Parameters:
file
- The specified file- Returns:
The byte array of the specified file
-
contentFile2byte
static Array<byte> contentFile2byte(Context context, Uri uri)
Retrieves the byte array of the specified file, which must start with content.
- Parameters:
context
- The context.uri
- The content URI.- Returns:
The byte array of the specified file.
-
byte2File
static File byte2File(Array<byte> buf, String filePath, String fileName)
Generates a file based on the byte array
- Parameters:
buf
- The byte arrayfilePath
- The file storage pathfileName
- The file name- Returns:
The generated file
-
getCachePath
static String getCachePath(Context context)
Retrieves the cache storage path, such as
/sdcard/Android/data/<package name>/cache
- Parameters:
context
- The provided Context- Returns:
The full path of the directory, or an empty string if external storage is not mounted or an error occurs
-
getFileKey
static String getFileKey(Message message)
Retrieves the key for media messages, used for handling messages such as images, videos, and GIFs.
- Parameters:
message
- The message object- Returns:
The key for the media message, used for local storage
-
getCachePath
static String getCachePath(Context context, String dir)
Retrieves the cache storage path, such as
/sdcard/Android/data/<package name>/cache/<dir>
- Parameters:
context
- The provided Contextdir
- The custom directory- Returns:
The full path of the directory, or an empty string if external storage is not mounted or an error occurs
-
getCacheDirsPath
static String getCacheDirsPath(Context context, String dir)
Retrieves the cache storage path, such as
/sdcard/Android/data/<package name>/cache/<dir>
- Parameters:
context
- The passed-in Contextdir
- Custom directory (multi-level directory)- Returns:
The full path of the directory, returns an empty string if external storage is not mounted or an error occurs
-
getTempFilePath
static String getTempFilePath(Context context, int messageId)
Get the temporary file storage path for resumable upload
- Parameters:
context
- The contextmessageId
- The message ID- Returns:
The save path
-
getTempFilePath
static String getTempFilePath(Context context, String id)
Get the temporary file storage path for resumable uploads
- Parameters:
context
- The contextid
- The unique identifier of the file- Returns:
The storage path
-
getInternalCachePath
static String getInternalCachePath(Context context, String dir)
Get the app cache storage path
data/data/<package name>/cache/<dir>
- Parameters:
context
- The passed-in Contextdir
- Custom directory- Returns:
The full path of the directory
-
getMediaDownloadDir
@Deprecated() static String getMediaDownloadDir(Context context)
Get the storage path of media files
- Parameters:
context
- The context- Returns:
The storage path of media files
-
getMediaDownloadDir
static String getMediaDownloadDir(Context context, String dir)
Get the storage path for media files
- Parameters:
context
- The contextdir
- Custom directory- Returns:
The storage path for media files
-
getFileSize
static long getFileSize(File file)
Get the size of the specified file
- Parameters:
file
- The specified file- Returns:
The length of the file
-
saveFile
static void saveFile(String str, String filePath)
Stores the string at the specified path
- Parameters:
str
- The string to be storedfilePath
- The specified path
-
getStringFromFile
static String getStringFromFile(String path)
Get file data
- Parameters:
path
- File path- Returns:
File data
-
removeFile
static void removeFile(String path)
Delete File
- Parameters:
path
- File path
-
getTempFileMD5
static String getTempFileMD5(Context context, int messageId)
Retrieves the MD5 key for file resumption, used to identify the file being downloaded. The MD5 is generated based on appKey, userId, and messageId.
- Parameters:
context
- The contextmessageId
- The message ID- Returns:
The save path
-
getTempFileMD5
static String getTempFileMD5(Context context, String tag)
Retrieves the temporary filename for resumable file download, used to identify the file being downloaded. The filename is generated by performing MD5 on the appKey, userId, and the unique identifier tag.
- Parameters:
context
- The contexttag
- The unique identifier for the file- Returns:
The save path
-
readPictureDegree
static int readPictureDegree(Context context, String path)
Reads the rotation angle of an image
- Parameters:
context
- The contextpath
- The absolute path of the image- Returns:
degree The rotation angle
-
isFileExistsWithUri
static boolean isFileExistsWithUri(Context pContext, Uri pUri)
Checks if the file exists.
- Parameters:
pContext
- The context.pUri
- The file URI.- Returns:
Indicates whether the file exists.
true
if the file exists,false
otherwise.
-
getFileLengthWithUri
static long getFileLengthWithUri(Context pContext, Uri pUri)
Gets the file length. Returns -1 if the file does not exist.
- Parameters:
pContext
- The contextpUri
- The file Uri- Returns:
Returns the file length if the file exists.
-
isValidateLocalUri
static boolean isValidateLocalUri(Uri pUri)
Checks if the file URI scheme starts with file or content.
- Parameters:
pUri
- The file URI- Returns:
Indicates whether the file URI scheme starts with file or content
-
uriStartWithFile
static boolean uriStartWithFile(Uri pUri)
Checks if the file Uri scheme starts with file and if the file Uri length exceeds 7
- Parameters:
pUri
- The file Uri- Returns:
Indicates whether the file Uri scheme starts with file and if the file Uri length exceeds 7
-
uriStartWithContent
static boolean uriStartWithContent(Uri srcUri)
Checks whether the file URI scheme starts with content.
- Parameters:
srcUri
- The file URI- Returns:
Indicates whether the file URI scheme starts with content
-
getSuffix
static String getSuffix(Uri srcUri)
Get the suffix of the Uri
- Parameters:
srcUri
- The file Uri- Returns:
The suffix may be empty
-
getSuffix
static String getSuffix(String path)
Get the URI suffix
- Parameters:
path
- path- Returns:
The suffix may be empty
-
getFileInfoByUri
static FileInfo getFileInfoByUri(Context context, Uri uri)
Retrieves the file name, size, and type from the URI.
- Parameters:
context
- The context.uri
- Supports URIs starting with 'content' and 'File'.- Returns:
ContentDocument Returns the file name, size, and type.
-
getFileInfoByFile
static FileInfo getFileInfoByFile(File file)
Retrieves the file name, size, and type from the file
- Returns:
ContentDocument Returns the file name, size, and type
-
getFileTypeFromInputStream
static String getFileTypeFromInputStream(InputStream inputStream)
Retrieves the file type from the input stream
- Parameters:
inputStream
- The input stream
-
getFileTypeFromByteData
static String getFileTypeFromByteData(Array<byte> data)
-
getFileInputStream
static InputStream getFileInputStream(Context context, Uri uri)
-
getExternalCacheDir
static File getExternalCacheDir(Context context)
// For versions below 9.0, getExternalCacheDir may throw an ArrayIndexOutOfBoundsException, which needs to be caught
-
generateKey
static String generateKey()
-
getJsonOutPath
static String getJsonOutPath(Context context)
-
sanitizeFilename
static String sanitizeFilename(String displayName)
Cleans the file name to prevent path traversal attacks
- Parameters:
displayName
- The file name- Returns:
The cleaned file name
-
getUrlFileName
static String getUrlFileName(String url, String fileName)
-
-
-
-