Class FileUtils

  • All Implemented Interfaces:

    
    public class FileUtils
    
                        
    • Constructor Detail

    • 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 Uri
        data - byte array
      • convertBitmap2File

         static File convertBitmap2File(Bitmap bm, String dir, String name)

        Converts a bitmap to a file.

        Parameters:
        bm - bitmap
        dir - storage directory path
        name - file name
        Returns:

        the output file

      • copyFile

         static File copyFile(File src, String path, String name)

        Copies a file.

        Parameters:
        src - source file
        path - destination directory path
        name - 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 path
        path - destination directory path
        name - destination file name
        Returns:

        whether the copy was successful

      • copyFileToInternal

         static boolean copyFileToInternal(Context context, Uri srcUri, String desPath, String name)
        Parameters:
        context - context
        srcUri - source URI starting with file:// or content://
        desPath - destination path under the app's package directory
        Returns:

        whether the file was copied successfully

      • resolveUniqueFileName

         static String resolveUniqueFileName(String dir, String name)

        Resolves a landing file name that does not collide with an existing file in dir. When name already exists, a numeric suffix is inserted before the extension (e.g. report.pdf ->report (1).pdf). This keeps consecutively sent files that share the same display name from overwriting each other in the internal cache directory.

        Parameters:
        dir - destination directory
        name - desired file name
        Returns:

        a file name unique within dir

      • copyFile

         static boolean copyFile(Context context, Uri srcUri, String desPath)
        Parameters:
        context - context
        srcUri - content:// URI obtained from MediaStore
        desPath - destination path under the app's package directory
        Returns:

        whether the file was copied successfully

      • 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 - context
        uri - 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 array
        filePath - destination directory path
        fileName - 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 - Context
        dir - 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 - Context
        dir - 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 - context
        messageId - message ID
        Returns:

        storage path

      • getTempFilePath

         static String getTempFilePath(Context context, String id)

        Gets the temporary file storage path for resumable downloads.

        Parameters:
        context - context
        id - 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 - Context
        dir - 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 - context
        dir - 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 save
        filePath - 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 - context
        messageId - 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 - context
        tag - unique file identifier
        Returns:

        the key

      • readPictureDegree

         static int readPictureDegree(Context context, String path)

        Reads the rotation degree of an image.

        Parameters:
        context - context
        path - absolute image path
        Returns:

        rotation degree

      • isFileExistsWithUri

         static boolean isFileExistsWithUri(Context pContext, Uri pUri)

        Checks whether a file exists.

        Parameters:
        pContext - context
        pUri - file Uri
        Returns:

        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 - context
        pUri - 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 - context
        uri - 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

      • getExternalCacheDir

         static File getExternalCacheDir(Context context)

        On versions below 9.0, getExternalCacheDir may throw ArrayIndexOutOfBoundsException

      • sanitizeFilename

         static String sanitizeFilename(String displayName)

        Sanitizes the file name to prevent path traversal attacks.

        Parameters:
        displayName - file name
        Returns:

        sanitized file name