Class DynamicFileUtils


  • public class DynamicFileUtils
    extends java.lang.Object
    Helper class to perform various File operations.

    A FileProvider in the form of ${applicationId}.FileProvider must be added in the manifest to perform some operations automatically like saving the bitmap or file in app isolated directory.

    See Also:
    Context.getExternalFilesDir(String)
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String ADU_DEFAULT_DIR_DATA
      Constant for the default data directory.
      static java.lang.String ADU_DEFAULT_DIR_TEMP
      Constant for the default temp directory.
      static java.lang.String ADU_MIME_OCTET_STREAM
      Constant for the application/octet-stream mime type.
      static java.lang.String FILE_MIME
      Default mime type for the file.
      static java.lang.String FILE_PROVIDER
      Default suffix for the file provider.
      static java.lang.String URI_MATCHER_CONTENT
      Constant to match the content URI.
      static java.lang.String URI_MATCHER_FILE
      Constant to match the file URI.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean deleteDirectory​(java.io.File dir)
      Delete a directory.
      static java.lang.String getBaseName​(java.lang.String fileName)
      Returns the base name without extension of given file name.
      static android.net.Uri getBitmapUri​(android.content.Context context, android.graphics.Bitmap bitmap, java.lang.String name)
      Save and returns URI from the bitmap.
      static android.net.Uri getBitmapUri​(android.content.Context context, android.graphics.Bitmap bitmap, java.lang.String name, java.lang.String extension)
      Save and returns URI from the bitmap.
      static java.io.File getDirectory​(java.lang.String type)
      Returns the default directory according to the supplied type if accessible.
      static java.io.File getDirectory​(java.lang.String type, java.lang.String fileName)
      Returns the default directory according to the supplied type if accessible.
      static java.lang.String getExtension​(java.io.File file)
      Returns the extension of a file.
      static java.lang.String getExtension​(java.lang.String fileName)
      Returns the extension of a file name.
      static java.lang.String getFileNameFromUri​(android.content.Context context, android.net.Uri uri)
      Returns file name from the URI.
      static android.content.Intent getFileSelectIntent​(java.lang.String mimeType)
      Returns an intent to select a file according to the mime type.
      static android.content.Intent getSaveToFileIntent​(android.content.Context context, android.net.Uri file, java.lang.String mimeType)
      Returns an intent to request a storage location for the supplied file.
      static android.content.Intent getSaveToFileIntent​(android.content.Context context, java.io.File file, java.lang.String mimeType)
      Returns an intent to request a storage location for the supplied file.
      static java.lang.String getTempDir​(android.content.Context context)
      Returns the default temp directory for the context.
      static android.net.Uri getUriFromFile​(android.content.Context context, java.io.File file)
      Returns URI from the file.
      static boolean isValidExtension​(android.content.Context context, android.content.Intent intent, java.lang.String extension)
      Checks whether the extension is valid for an intent.
      static boolean isValidExtension​(android.content.Context context, android.net.Uri uri, java.lang.String extension)
      Checks whether the extension is valid for a URI.
      static boolean isValidExtension​(android.content.Context context, java.io.File file, java.lang.String extension)
      Checks whether the extension is valid for a file.
      static boolean isValidExtension​(java.lang.String path, java.lang.String extension)
      Checks whether the extension is valid for a path.
      static boolean isValidMimeType​(android.content.Context context, android.content.Intent intent, java.lang.String mimeType, java.lang.String extension)
      Checks whether the mime type is valid for an intent data.
      static boolean isValidMimeType​(android.content.Context context, android.net.Uri uri, java.lang.String mimeType, java.lang.String extension)
      Checks whether the mime type is valid for a URI.
      static boolean isValidMimeType​(android.content.Context context, java.io.File file, java.lang.String mimeType, java.lang.String extension)
      Checks whether the mime type is valid for a file.
      static java.lang.String readStringFromFile​(android.content.Context context, android.net.Uri fileUri)
      Reads a string data from the file URI.
      static void shareFile​(android.app.Activity activity, java.lang.String title, java.lang.String subject, java.io.File file, java.lang.String mimeType)
      Share file according to the mime type.
      static void shareFiles​(android.app.Activity activity, java.lang.String title, java.lang.String subject, android.net.Uri[] uris, java.lang.String mimeType)
      Share multiple files.
      static void unzip​(java.io.File zip, java.io.File extractTo)
      Extracts a zip archive.
      static boolean verifyFile​(java.io.File file)
      Verifies a file if it exist or not.
      static boolean writeStringToFile​(android.content.Context context, java.lang.String data, android.net.Uri sourceUri)
      Writes a string data to file URI.
      static boolean writeStringToFile​(android.content.Context context, java.lang.String data, android.net.Uri sourceUri, android.net.Uri destinationUri)
      Writes a string data to file URI from the source to destination in "rwt" mode which truncates all the previous content if the destination URI already exists.
      static boolean writeStringToFile​(android.content.Context context, java.lang.String data, android.net.Uri sourceUri, android.net.Uri destinationUri, java.lang.String mode)
      Writes a string data to file URI from the source to destination.
      static boolean writeToFile​(android.content.Context context, android.net.Uri sourceUri, android.net.Uri destinationUri)
      Writes a file URI from the source to destination in "rwt" mode which truncates all the previous content if the destination URI already exists.
      static boolean writeToFile​(android.content.Context context, android.net.Uri sourceUri, android.net.Uri destinationUri, java.lang.String mode)
      Writes a file URI from the source to destination.
      static boolean writeToFile​(java.io.File source, java.io.File destination, java.lang.String outputFileName)
      Writes a file from the source to destination.
      static void zipDirectory​(java.io.File dir, java.io.File zip)
      Creates a zip archive from the directory.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • FILE_MIME

        public static final java.lang.String FILE_MIME
        Default mime type for the file.
        See Also:
        Constant Field Values
      • FILE_PROVIDER

        public static final java.lang.String FILE_PROVIDER
        Default suffix for the file provider.
        See Also:
        Constant Field Values
      • URI_MATCHER_CONTENT

        public static final java.lang.String URI_MATCHER_CONTENT
        Constant to match the content URI.
        See Also:
        Constant Field Values
      • URI_MATCHER_FILE

        public static final java.lang.String URI_MATCHER_FILE
        Constant to match the file URI.
        See Also:
        Constant Field Values
      • ADU_DEFAULT_DIR_DATA

        public static final java.lang.String ADU_DEFAULT_DIR_DATA
        Constant for the default data directory.
        See Also:
        Constant Field Values
      • ADU_DEFAULT_DIR_TEMP

        public static final java.lang.String ADU_DEFAULT_DIR_TEMP
        Constant for the default temp directory.
        See Also:
        Constant Field Values
      • ADU_MIME_OCTET_STREAM

        public static final java.lang.String ADU_MIME_OCTET_STREAM
        Constant for the application/octet-stream mime type.
        See Also:
        Constant Field Values
    • Constructor Detail

      • DynamicFileUtils

        public DynamicFileUtils()
    • Method Detail

      • getTempDir

        @Nullable
        public static java.lang.String getTempDir​(@NonNull
                                                  android.content.Context context)
        Returns the default temp directory for the context.
        Parameters:
        context - The context to get the package name.
        Returns:
        The default temp directory for the context.
      • getDirectory

        @Nullable
        public static java.io.File getDirectory​(@NonNull
                                                java.lang.String type)
        Returns the default directory according to the supplied type if accessible.
        Parameters:
        type - The type of the directory.
        Returns:
        The default directory according to the supplied type if accessible.
      • getDirectory

        @Nullable
        public static java.io.File getDirectory​(@NonNull
                                                java.lang.String type,
                                                @Nullable
                                                java.lang.String fileName)
        Returns the default directory according to the supplied type if accessible.
        Parameters:
        type - The type of the directory.
        fileName - The optional file name.
        Returns:
        The default directory according to the supplied type if accessible.
      • getBaseName

        @Nullable
        public static java.lang.String getBaseName​(@Nullable
                                                   java.lang.String fileName)
        Returns the base name without extension of given file name.

        e.g. getBaseName("file.txt") will return "file".

        Parameters:
        fileName - The full name of the file with extension.
        Returns:
        The base name of the file without extension.
      • getExtension

        @Nullable
        public static java.lang.String getExtension​(@Nullable
                                                    java.lang.String fileName)
        Returns the extension of a file name.
        Parameters:
        fileName - The file name to retrieve its extension.
        Returns:
        The extension of the file name.
      • getExtension

        @Nullable
        public static java.lang.String getExtension​(@Nullable
                                                    java.io.File file)
        Returns the extension of a file.
        Parameters:
        file - The file to retrieve its extension.
        Returns:
        The extension of the file.
      • verifyFile

        public static boolean verifyFile​(@Nullable
                                         java.io.File file)
        Verifies a file if it exist or not.
        Parameters:
        file - The file to be verified.
        Returns:
        true if a file can be accessed by automatically creating the sub directories.
      • deleteDirectory

        public static boolean deleteDirectory​(@NonNull
                                              java.io.File dir)
        Delete a directory.
        Parameters:
        dir - The directory to be deleted.
        Returns:
        true if the directory has been deleted successfully.
      • zipDirectory

        public static void zipDirectory​(@NonNull
                                        java.io.File dir,
                                        @NonNull
                                        java.io.File zip)
                                 throws java.io.IOException
        Creates a zip archive from the directory.
        Parameters:
        dir - The directory to be archived.
        zip - The output zip archive.
        Throws:
        java.io.IOException - Throws IO exception.
      • unzip

        public static void unzip​(@NonNull
                                 java.io.File zip,
                                 @NonNull
                                 java.io.File extractTo)
                          throws java.lang.SecurityException,
                                 java.io.IOException
        Extracts a zip archive.
        Parameters:
        zip - The zip archive to be extracted.
        extractTo - The unzip destination.
        Throws:
        java.io.IOException - Throws IO exception.
        java.util.zip.ZipException - Throws Zip exception.
        java.lang.SecurityException
      • getUriFromFile

        @Nullable
        public static android.net.Uri getUriFromFile​(@NonNull
                                                     android.content.Context context,
                                                     @Nullable
                                                     java.io.File file)
        Returns URI from the file.

        It will automatically use the @link FileProvider} on API 24 and above.

        Parameters:
        context - The context to get the file provider.
        file - The file to get the URI.
        Returns:
        The URI from the file.
        See Also:
        Uri
      • getFileNameFromUri

        @Nullable
        public static java.lang.String getFileNameFromUri​(@NonNull
                                                          android.content.Context context,
                                                          @Nullable
                                                          android.net.Uri uri)
        Returns file name from the URI.
        Parameters:
        context - The context to get content resolver.
        uri - The URI to get the file name.
        Returns:
        The file name from the URI.
        See Also:
        Context.getContentResolver()
      • writeToFile

        public static boolean writeToFile​(@NonNull
                                          java.io.File source,
                                          @NonNull
                                          java.io.File destination,
                                          @NonNull
                                          java.lang.String outputFileName)
        Writes a file from the source to destination.
        Parameters:
        source - The source file.
        destination - The destination file.
        outputFileName - The output files name.
        Returns:
        true if the file has been written successfully.
      • writeToFile

        public static boolean writeToFile​(@NonNull
                                          android.content.Context context,
                                          @Nullable
                                          android.net.Uri sourceUri,
                                          @Nullable
                                          android.net.Uri destinationUri,
                                          @NonNull
                                          java.lang.String mode)
        Writes a file URI from the source to destination.
        Parameters:
        context - The context to get content resolver.
        sourceUri - The source file URI.
        destinationUri - The destination file URI.
        mode - Mode for the destination file.

        May be "w", "wa", "rw", or "rwt".

        Returns:
        true if the file has been written successfully.
      • writeToFile

        public static boolean writeToFile​(@NonNull
                                          android.content.Context context,
                                          @Nullable
                                          android.net.Uri sourceUri,
                                          @Nullable
                                          android.net.Uri destinationUri)
        Writes a file URI from the source to destination in "rwt" mode which truncates all the previous content if the destination URI already exists.
        Parameters:
        context - The context to get content resolver.
        sourceUri - The source file URI.
        destinationUri - The destination file URI.
        Returns:
        true if the file has been written successfully.
        See Also:
        writeToFile(Context, Uri, Uri, String)
      • writeStringToFile

        public static boolean writeStringToFile​(@NonNull
                                                android.content.Context context,
                                                @Nullable
                                                java.lang.String data,
                                                @Nullable
                                                android.net.Uri sourceUri,
                                                @Nullable
                                                android.net.Uri destinationUri,
                                                @NonNull
                                                java.lang.String mode)
        Writes a string data to file URI from the source to destination.
        Parameters:
        context - The context to get content resolver.
        data - The string data to be written.
        sourceUri - The source file URI.
        destinationUri - The destination file URI.
        mode - Mode for the destination file.

        May be "w", "wa", "rw", or "rwt".

        Returns:
        true if the file has been written successfully.
      • writeStringToFile

        public static boolean writeStringToFile​(@NonNull
                                                android.content.Context context,
                                                @Nullable
                                                java.lang.String data,
                                                @Nullable
                                                android.net.Uri sourceUri,
                                                @Nullable
                                                android.net.Uri destinationUri)
        Writes a string data to file URI from the source to destination in "rwt" mode which truncates all the previous content if the destination URI already exists.
        Parameters:
        context - The context to get content resolver.
        data - The string data to be written.
        sourceUri - The source file URI.
        destinationUri - The destination file URI.
        Returns:
        true if the file has been written successfully.
        See Also:
        writeStringToFile(Context, String, Uri, Uri, String)
      • writeStringToFile

        public static boolean writeStringToFile​(@NonNull
                                                android.content.Context context,
                                                @Nullable
                                                java.lang.String data,
                                                @Nullable
                                                android.net.Uri sourceUri)
        Writes a string data to file URI.
        Parameters:
        context - The context to get content resolver.
        data - The string data to be written.
        sourceUri - The source file URI.
        Returns:
        true if the file has been written successfully.
        See Also:
        writeStringToFile(Context, String, Uri, Uri)
      • readStringFromFile

        @Nullable
        public static java.lang.String readStringFromFile​(@NonNull
                                                          android.content.Context context,
                                                          @NonNull
                                                          android.net.Uri fileUri)
        Reads a string data from the file URI.
        Parameters:
        context - The context to get content resolver.
        fileUri - The source file URI.
        Returns:
        The string data after reading the file.
      • getBitmapUri

        @Nullable
        public static android.net.Uri getBitmapUri​(@NonNull
                                                   android.content.Context context,
                                                   @Nullable
                                                   android.graphics.Bitmap bitmap,
                                                   @NonNull
                                                   java.lang.String name,
                                                   @Nullable
                                                   java.lang.String extension)
        Save and returns URI from the bitmap.

        It will automatically use the @link FileProvider} on API 24 and above.

        It requires Manifest.permission.WRITE_EXTERNAL_STORAGE permission on API 18 and below.

        Parameters:
        context - The context to get the file provider.
        bitmap - The bitmap to get the URI.
        name - The name for the file.
        extension - The extension for the file.
        Returns:
        The URI from the bitmap.
        See Also:
        Uri
      • getBitmapUri

        @Nullable
        public static android.net.Uri getBitmapUri​(@NonNull
                                                   android.content.Context context,
                                                   @Nullable
                                                   android.graphics.Bitmap bitmap,
                                                   @NonNull
                                                   java.lang.String name)
        Save and returns URI from the bitmap.

        It will automatically use the @link FileProvider} on API 24 and above.

        It requires Manifest.permission.WRITE_EXTERNAL_STORAGE permission on API 18 and below.

        Parameters:
        context - The context to get the file provider.
        bitmap - The bitmap to get the URI.
        name - The name for the file.
        Returns:
        The URI from the bitmap.
        See Also:
        Uri
      • isValidExtension

        public static boolean isValidExtension​(@Nullable
                                               java.lang.String path,
                                               @Nullable
                                               java.lang.String extension)
        Checks whether the extension is valid for a path.
        Parameters:
        path - The path string to get the extension.
        extension - The extension to be validated.
        Returns:
        true if the extension is valid for the path.
      • isValidExtension

        public static boolean isValidExtension​(@Nullable
                                               android.content.Context context,
                                               @Nullable
                                               java.io.File file,
                                               @Nullable
                                               java.lang.String extension)
        Checks whether the extension is valid for a file.
        Parameters:
        context - The context to retrieve the resources.
        file - The file to get the extension.
        extension - The extension to be validated.
        Returns:
        true if the extension is valid for the file.
      • isValidExtension

        public static boolean isValidExtension​(@Nullable
                                               android.content.Context context,
                                               @Nullable
                                               android.net.Uri uri,
                                               @Nullable
                                               java.lang.String extension)
        Checks whether the extension is valid for a URI.
        Parameters:
        context - The context to retrieve the resources.
        uri - The URI to get the extension.
        extension - The extension to be validated.
        Returns:
        true if the extension is valid for the URI.
      • isValidExtension

        public static boolean isValidExtension​(@Nullable
                                               android.content.Context context,
                                               @Nullable
                                               android.content.Intent intent,
                                               @Nullable
                                               java.lang.String extension)
        Checks whether the extension is valid for an intent.
        Parameters:
        context - The context to retrieve the resources.
        intent - The intent to get the extension.
        extension - The extension to be validated.
        Returns:
        true if the extension is valid for the intent.
      • isValidMimeType

        public static boolean isValidMimeType​(@Nullable
                                              android.content.Context context,
                                              @Nullable
                                              android.content.Intent intent,
                                              @NonNull
                                              java.lang.String mimeType,
                                              @Nullable
                                              java.lang.String extension)
        Checks whether the mime type is valid for an intent data.
        Parameters:
        context - The context to match the URI mime type.
        intent - The intent to get the data.
        mimeType - The mime type to be validated.
        extension - The optional extension to be validated if mime type is invalid.
        Returns:
        true if the mime type is valid for the intent data.
      • isValidMimeType

        public static boolean isValidMimeType​(@Nullable
                                              android.content.Context context,
                                              @Nullable
                                              android.net.Uri uri,
                                              @NonNull
                                              java.lang.String mimeType,
                                              @Nullable
                                              java.lang.String extension)
        Checks whether the mime type is valid for a URI.
        Parameters:
        context - The context to get the content resolver.
        uri - The URI to get the type.
        mimeType - The mime type to be validated.
        extension - The optional extension to be validated if mime type is invalid.
        Returns:
        true if the mime type is valid for the intent data.
      • isValidMimeType

        public static boolean isValidMimeType​(@NonNull
                                              android.content.Context context,
                                              @Nullable
                                              java.io.File file,
                                              @NonNull
                                              java.lang.String mimeType,
                                              @Nullable
                                              java.lang.String extension)
        Checks whether the mime type is valid for a file.
        Parameters:
        context - The context to get the content resolver.
        file - The file to get the URI.
        mimeType - The mime type to be validated.
        extension - The optional extension to be validated if mime type is invalid.
        Returns:
        true if the mime type is valid for the intent data.
      • shareFile

        public static void shareFile​(@NonNull
                                     android.app.Activity activity,
                                     @Nullable
                                     java.lang.String title,
                                     @Nullable
                                     java.lang.String subject,
                                     @NonNull
                                     java.io.File file,
                                     @NonNull
                                     java.lang.String mimeType)
        Share file according to the mime type.
        Parameters:
        activity - The activity to create the intent chooser.
        title - The title for the intent chooser.
        subject - The subject for the intent chooser.
        file - The file to be shared.
        mimeType - The mime type of the file.
        Throws:
        android.content.ActivityNotFoundException - The activity not found exception.
      • shareFiles

        public static void shareFiles​(@NonNull
                                      android.app.Activity activity,
                                      @Nullable
                                      java.lang.String title,
                                      @Nullable
                                      java.lang.String subject,
                                      @NonNull
                                      android.net.Uri[] uris,
                                      @Nullable
                                      java.lang.String mimeType)
        Share multiple files.
        Parameters:
        activity - The activity to create the intent chooser.
        title - The title for the intent chooser.
        subject - The subject for the intent chooser.
        uris - The content uris to be shared.
        mimeType - The mime type of the file.
        Throws:
        android.content.ActivityNotFoundException - The activity not found exception.
      • getSaveToFileIntent

        @NonNull
        public static android.content.Intent getSaveToFileIntent​(@NonNull
                                                                 android.content.Context context,
                                                                 @Nullable
                                                                 android.net.Uri file,
                                                                 @NonNull
                                                                 java.lang.String mimeType)
        Returns an intent to request a storage location for the supplied file.
        Parameters:
        context - The context to get the file URI.
        file - The file URI to request the storage location.
        mimeType - The mime type of the file.
        Returns:
        The intent to request a storage location for the supplied file.
      • getSaveToFileIntent

        @NonNull
        public static android.content.Intent getSaveToFileIntent​(@NonNull
                                                                 android.content.Context context,
                                                                 @Nullable
                                                                 java.io.File file,
                                                                 @NonNull
                                                                 java.lang.String mimeType)
        Returns an intent to request a storage location for the supplied file.
        Parameters:
        context - The context to get the file URI.
        file - The file to request the storage location.
        mimeType - The mime type of the file.
        Returns:
        The intent to request a storage location for the supplied file.
      • getFileSelectIntent

        @NonNull
        public static android.content.Intent getFileSelectIntent​(@NonNull
                                                                 java.lang.String mimeType)
        Returns an intent to select a file according to the mime type.
        Parameters:
        mimeType - The mime type for the file.
        Returns:
        The intent intent to select a file according to the mime type.