Class FileUtils

  • All Implemented Interfaces:

    
    public class FileUtils
    
                        
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
      public final static int FILE_SCHEME_LENGTH
    • Constructor Summary

      Constructors 
      Constructor Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      static InputStream getFileInputStream(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 void writeByte(Uri uri, Array<byte> data) Writes byte array sequentially to a file output stream.
      static File convertBitmap2File(Bitmap bm, String dir, String name) Converts a bitmap to a file.
      static File copyFile(File src, String path, String name) Copies a file.
      static boolean copyFile(String srcPath, String path, String name) Copies a 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) 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 File byte2File(Array<byte> buf, String filePath, String fileName) Creates a file from a byte array.
      static String getCachePath(Context context) Gets the cache storage path, e.g.
      static String getFileKey(Object message) Gets the key for a media message, used for image, video, GIF message handlers.
      static String getCachePath(Context context, String dir) Gets the cache storage path, e.g.
      static String getCacheDirsPath(Context context, String dir) Gets the cache storage path, e.g.
      static String getTempFilePath(Context context, int messageId) Gets the temporary file storage path for resumable downloads.
      static String getTempFilePath(Context context, String id) Gets the temporary file storage path for resumable downloads.
      static String getInternalCachePath(Context context, String dir) Gets the app internal cache path data/data/<package name>/cache/<dir>
      static String getMediaDownloadDir(Context context) Gets the media file storage directory.
      static String getMediaDownloadDir(Context context, String dir) Gets the media file storage directory.
      static long getFileSize(File file) Gets the size of the specified file.
      static void saveFile(String str, String filePath) Saves a string to the specified file path.
      static String getStringFromFile(String path) Reads file content as a string.
      static void removeFile(String path) Deletes a file.
      static String getTempFileMD5(Context context, int messageId) Gets the MD5 key for resumable file download, used to identify the file being downloaded.
      static String getTempFileMD5(Context context, String tag) Gets the temporary file name for resumable download, used to identify the file being downloaded.
      static int readPictureDegree(Context context, String path) Reads the rotation degree of an image.
      static boolean isFileExistsWithUri(Context pContext, Uri pUri) Checks whether a file exists.
      static long getFileLengthWithUri(Context pContext, Uri pUri) Gets the file length.
      static boolean isValidateLocalUri(Uri pUri) Checks whether the file Uri scheme starts with "file" or "content".
      static boolean uriStartWithFile(Uri pUri) Checks whether the file Uri scheme starts with "file" and its length exceeds 7.
      static boolean uriStartWithContent(Uri srcUri) Checks whether the file Uri scheme starts with "content".
      static String getSuffix(Uri srcUri) Gets the file extension from a Uri.
      static String getSuffix(String path) Gets the file extension from a path.
      static FileInfo getFileInfoByUri(Context context, Uri uri) Gets file name, size, and type from a URI.
      static FileInfo getFileInfoByFile(File file) Gets file name, size, and type from a File object.
      static String getFileTypeFromInputStream(InputStream inputStream) Detects the file type from an input stream.
      static String getFileTypeFromByteData(Array<byte> data)
      static InputStream getFileInputStream(Context context, Uri uri)
      static File getExternalCacheDir(Context context) On versions below 9.
      static String generateKey()
      static String getJsonOutPath(Context context)
      static String sanitizeFilename(String displayName) Sanitizes the file name to prevent path traversal attacks.
      static String getUrlFileName(String url, String fileName)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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

      • 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

      • 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 - 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

      • getFileTypeFromInputStream

         static String getFileTypeFromInputStream(InputStream inputStream)

        Detects the file type from an input stream.

        Parameters:
        inputStream - input stream
        Returns:

        file type extension

      • 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

      • getUrlFileName

         static String getUrlFileName(String url, String fileName)