Class FileUtils
- 
                    
                    - All Implemented Interfaces:
 
 public class FileUtils
- 
                
                    
                    - 
                                
                            
                                Field SummaryFields Modifier and Type Field Description public final static intFILE_SCHEME_LENGTH
 - 
                                
                            
                                Method SummaryModifier and Type Method Description static InputStreamgetFileInputStream(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 voidwriteByte(Uri uri, Array<byte> data)Writes the byte array sequentially to the file output stream. static FileconvertBitmap2File(Bitmap bm, String dir, String name)Convert image to file static FilecopyFile(File src, String path, String name)Copy a file static booleancopyFile(String srcPath, String path, String name)Copy files, only supports files starting with 'file' static booleancopyFileToInternal(Context context, Uri srcUri, String desPath, String name)static booleancopyFile(Context context, Uri srcUri, String desPath)static StringgetFileNameWithPath(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 Filebyte2File(Array<byte> buf, String filePath, String fileName)Generates a file based on the byte array static StringgetCachePath(Context context)Retrieves the cache storage path, such as /sdcard/Android/data/<package name>/cachestatic StringgetFileKey(Message message)Retrieves the key for media messages, used for handling messages such as images, videos, and GIFs. static StringgetCachePath(Context context, String dir)Retrieves the cache storage path, such as /sdcard/Android/data/<package name>/cache/<dir>static StringgetCacheDirsPath(Context context, String dir)Retrieves the cache storage path, such as /sdcard/Android/data/<package name>/cache/<dir>static StringgetTempFilePath(Context context, int messageId)Get the temporary file storage path for resumable upload static StringgetTempFilePath(Context context, String id)Get the temporary file storage path for resumable uploads static StringgetInternalCachePath(Context context, String dir)Get the app cache storage path data/data/<package name>/cache/<dir>static StringgetMediaDownloadDir(Context context)Get the storage path of media files static StringgetMediaDownloadDir(Context context, String dir)Get the storage path for media files static longgetFileSize(File file)Get the size of the specified file static voidsaveFile(String str, String filePath)Stores the string at the specified path static StringgetStringFromFile(String path)Get file data static voidremoveFile(String path)Delete File static StringgetTempFileMD5(Context context, int messageId)Retrieves the MD5 key for file resumption, used to identify the file being downloaded. static StringgetTempFileMD5(Context context, String tag)Retrieves the temporary filename for resumable file download, used to identify the file being downloaded. static intreadPictureDegree(Context context, String path)Reads the rotation angle of an image static booleanisFileExistsWithUri(Context pContext, Uri pUri)Checks if the file exists. static longgetFileLengthWithUri(Context pContext, Uri pUri)Gets the file length. static booleanisValidateLocalUri(Uri pUri)Checks if the file URI scheme starts with file or content. static booleanuriStartWithFile(Uri pUri)Checks if the file Uri scheme starts with file and if the file Uri length exceeds 7 static booleanuriStartWithContent(Uri srcUri)Checks whether the file URI scheme starts with content. static StringgetSuffix(Uri srcUri)Get the suffix of the Uri static StringgetSuffix(String path)Get the URI suffix static FileInfogetFileInfoByUri(Context context, Uri uri)Retrieves the file name, size, and type from the URI. static FileInfogetFileInfoByFile(File file)Retrieves the file name, size, and type from the file static StringgetFileTypeFromInputStream(InputStream inputStream)Retrieves the file type from the input stream static StringgetFileTypeFromByteData(Array<byte> data)static InputStreamgetFileInputStream(Context context, Uri uri)static FilegetExternalCacheDir(Context context)// For versions below 9. static StringgenerateKey()static StringgetJsonOutPath(Context context)static StringsanitizeFilename(String displayName)Cleans the file name to prevent path traversal attacks static StringgetUrlFileName(String url, String fileName)- 
                    
                    
                    - 
                                
                            
                                Method Detail- 
                                        getFileInputStreamstatic 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. 
 
 - 
                                        getByteFromUristatic 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 
 
 - 
                                        writeBytestatic void writeByte(Uri uri, Array<byte> data) Writes the byte array sequentially to the file output stream. - Parameters:
- uri- The file URI
- data- The byte array
 
 - 
                                        convertBitmap2Filestatic File convertBitmap2File(Bitmap bm, String dir, String name) Convert image to file - Parameters:
- bm- The image
- dir- The storage path for the image
- name- The image name
- Returns:
- The file 
 
 - 
                                        copyFilestatic File copyFile(File src, String path, String name) Copy a file - Parameters:
- src- Source file
- path- Destination file path
- name- Destination file name
- Returns:
- Destination file 
 
 - 
                                        copyFilestatic boolean copyFile(String srcPath, String path, String name) Copy files, only supports files starting with 'file' - Parameters:
- srcPath- Source file path
- path- Destination file path
- name- Destination file name
- Returns:
- Indicates whether the copy operation was successful 
 
 - 
                                        copyFileToInternalstatic boolean copyFileToInternal(Context context, Uri srcUri, String desPath, String name) - Parameters:
- context- The context
- srcUri- Starts with file or content
- desPath- 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 
 
 - 
                                        copyFilestatic boolean copyFile(Context context, Uri srcUri, String desPath) - Parameters:
- context- Context
- srcUri- 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 
 
 - 
                                        getFileNameWithPathstatic String getFileNameWithPath(String path) Retrieves the file name based on the file path - Parameters:
- path- The file path
- Returns:
- The file name 
 
 - 
                                        file2bytestatic 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 
 
 - 
                                        contentFile2bytestatic 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. 
 
 - 
                                        byte2Filestatic File byte2File(Array<byte> buf, String filePath, String fileName) Generates a file based on the byte array - Parameters:
- buf- The byte array
- filePath- The file storage path
- fileName- The file name
- Returns:
- The generated file 
 
 - 
                                        getCachePathstatic 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 
 
 - 
                                        getFileKeystatic 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 
 
 - 
                                        getCachePathstatic String getCachePath(Context context, String dir) Retrieves the cache storage path, such as /sdcard/Android/data/<package name>/cache/<dir>- Parameters:
- context- The provided Context
- dir- The custom directory
- Returns:
- The full path of the directory, or an empty string if external storage is not mounted or an error occurs 
 
 - 
                                        getCacheDirsPathstatic 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 Context
- dir- 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 
 
 - 
                                        getTempFilePathstatic String getTempFilePath(Context context, int messageId) Get the temporary file storage path for resumable upload - Parameters:
- context- The context
- messageId- The message ID
- Returns:
- The save path 
 
 - 
                                        getTempFilePathstatic String getTempFilePath(Context context, String id) Get the temporary file storage path for resumable uploads - Parameters:
- context- The context
- id- The unique identifier of the file
- Returns:
- The storage path 
 
 - 
                                        getInternalCachePathstatic String getInternalCachePath(Context context, String dir) Get the app cache storage path data/data/<package name>/cache/<dir>- Parameters:
- context- The passed-in Context
- dir- 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 
 
 - 
                                        getMediaDownloadDirstatic String getMediaDownloadDir(Context context, String dir) Get the storage path for media files - Parameters:
- context- The context
- dir- Custom directory
- Returns:
- The storage path for media files 
 
 - 
                                        getFileSizestatic long getFileSize(File file) Get the size of the specified file - Parameters:
- file- The specified file
- Returns:
- The length of the file 
 
 - 
                                        saveFilestatic void saveFile(String str, String filePath) Stores the string at the specified path - Parameters:
- str- The string to be stored
- filePath- The specified path
 
 - 
                                        getStringFromFilestatic String getStringFromFile(String path) Get file data - Parameters:
- path- File path
- Returns:
- File data 
 
 - 
                                        removeFilestatic void removeFile(String path) Delete File - Parameters:
- path- File path
 
 - 
                                        getTempFileMD5static 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 context
- messageId- The message ID
- Returns:
- The save path 
 
 - 
                                        getTempFileMD5static 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 context
- tag- The unique identifier for the file
- Returns:
- The save path 
 
 - 
                                        readPictureDegreestatic int readPictureDegree(Context context, String path) Reads the rotation angle of an image - Parameters:
- context- The context
- path- The absolute path of the image
- Returns:
- degree The rotation angle 
 
 - 
                                        isFileExistsWithUristatic 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. - trueif the file exists,- falseotherwise.
 
 - 
                                        getFileLengthWithUristatic long getFileLengthWithUri(Context pContext, Uri pUri) Gets the file length. Returns -1 if the file does not exist. - Parameters:
- pContext- The context
- pUri- The file Uri
- Returns:
- Returns the file length if the file exists. 
 
 - 
                                        isValidateLocalUristatic 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 
 
 - 
                                        uriStartWithFilestatic 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 
 
 - 
                                        uriStartWithContentstatic 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 
 
 - 
                                        getSuffixstatic String getSuffix(Uri srcUri) Get the suffix of the Uri - Parameters:
- srcUri- The file Uri
- Returns:
- The suffix may be empty 
 
 - 
                                        getSuffixstatic String getSuffix(String path) Get the URI suffix - Parameters:
- path- path
- Returns:
- The suffix may be empty 
 
 - 
                                        getFileInfoByUristatic 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. 
 
 - 
                                        getFileInfoByFilestatic FileInfo getFileInfoByFile(File file) Retrieves the file name, size, and type from the file - Returns:
- ContentDocument Returns the file name, size, and type 
 
 - 
                                        getFileTypeFromInputStreamstatic String getFileTypeFromInputStream(InputStream inputStream) Retrieves the file type from the input stream - Parameters:
- inputStream- The input stream
 
 - 
                                        getFileTypeFromByteDatastatic String getFileTypeFromByteData(Array<byte> data) 
 - 
                                        getFileInputStreamstatic InputStream getFileInputStream(Context context, Uri uri) 
 - 
                                        getExternalCacheDirstatic File getExternalCacheDir(Context context) // For versions below 9.0, getExternalCacheDir may throw an ArrayIndexOutOfBoundsException, which needs to be caught 
 - 
                                        generateKeystatic String generateKey() 
 - 
                                        getJsonOutPathstatic String getJsonOutPath(Context context) 
 - 
                                        sanitizeFilenamestatic String sanitizeFilename(String displayName) Cleans the file name to prevent path traversal attacks - Parameters:
- displayName- The file name
- Returns:
- The cleaned file name 
 
 - 
                                        getUrlFileNamestatic String getUrlFileName(String url, String fileName) 
 
- 
                                        
 
- 
                                
                            
                                
 
- 
                    
                    
                    
 
-