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 DIR_DATA
      Constant for the default data directory.
      static java.lang.String DIR_TEMP
      Constant for the default temp directory.
      static java.lang.String EXTENSION_IMAGE
      Default file extension for the image.
      static java.lang.String FILE_PROVIDER
      Default suffix for the file provider.
      static java.lang.String MIME_ALL
      Constant for the any mime type.
      static java.lang.String MIME_APPLICATION
      Constant for the application/* mime type.
      static java.lang.String MIME_OCTET_STREAM
      Constant for the application/octet-stream mime type.
      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)
      Deletes a directory.
      static java.lang.String getBaseName​(java.lang.String fileName)
      Returns the base name without extension of given file name.
      static Uri getBitmapUri​(Context context, Bitmap bitmap, java.lang.String name)
      Save and returns URI from the bitmap.
      static Uri getBitmapUri​(Context context, Bitmap bitmap, java.lang.String name, java.lang.String extension)
      Save and returns URI from the bitmap.
      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 getExternalDir​(Context context, java.lang.String path)
      Returns the default external directory for the app appended with the supplied path.
      static java.lang.String getFileNameFromUri​(Context context, Uri uri)
      Returns file name from the URI.
      static Intent getFileSelectIntent​(java.lang.String mimeType)
      Returns an intent to select a file according to the mime type.
      static java.io.File getPublicDir​(java.lang.String type)
      Returns the default directory according to the supplied type if accessible.
      static java.io.File getPublicDir​(java.lang.String type, java.lang.String fileName)
      Returns the default directory according to the supplied type if accessible.
      static Intent getSaveToFileIntent​(Context context, Uri file, java.lang.String mimeType)
      Returns an intent to request a storage location for the supplied file.
      static Intent getSaveToFileIntent​(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​(Context context)
      Returns the default temp directory for the context.
      static Uri getUriFromFile​(Context context, java.io.File file)
      Returns URI from the file.
      static boolean isValidExtension​(Context context, Intent intent, java.lang.String extension)
      Checks whether the extension is valid for an intent.
      static boolean isValidExtension​(Context context, Uri uri, java.lang.String extension)
      Checks whether the extension is valid for a URI.
      static boolean isValidExtension​(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​(Context context, Intent intent, java.lang.String mimeType, java.lang.String extension)
      Checks whether the mime type is valid for an intent data.
      static boolean isValidMimeType​(Context context, Uri uri, java.lang.String mimeType, java.lang.String extension)
      Checks whether the mime type is valid for a URI.
      static boolean isValidMimeType​(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​(Context context, Uri fileUri)
      Reads a string data from the file URI.
      static void shareFile​(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​(Activity activity, java.lang.String title, java.lang.String subject, Uri[] uris, java.lang.String mimeType)
      Share multiple files.
      static void takePersistedUriPermission​(Context context, Intent data)
      Try to take the persistable storage permission for the intent data URI.
      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 exists or not.
      static boolean writeStringToFile​(Context context, java.lang.String data, Uri sourceUri)
      Writes a string data to file URI.
      static boolean writeStringToFile​(Context context, java.lang.String data, Uri sourceUri, 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​(Context context, java.lang.String data, Uri sourceUri, Uri destinationUri, java.lang.String mode)
      Writes a string data to file URI from the source to destination.
      static boolean writeToFile​(Context context, Uri sourceUri, 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​(Context context, Uri sourceUri, 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

      • MIME_ALL

        public static final java.lang.String MIME_ALL
        Constant for the any mime type.
        See Also:
        Constant Field Values
      • MIME_APPLICATION

        public static final java.lang.String MIME_APPLICATION
        Constant for the application/* mime type.
        See Also:
        Constant Field Values
      • MIME_OCTET_STREAM

        public static final java.lang.String MIME_OCTET_STREAM
        Constant for the application/octet-stream mime type.
        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
      • DIR_DATA

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

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

        public static final java.lang.String EXTENSION_IMAGE
        Default file extension for the image.
        See Also:
        Constant Field Values
    • Constructor Detail

      • DynamicFileUtils

        public DynamicFileUtils()
    • Method Detail

      • getExternalDir

        @Nullable
        public static java.lang.String getExternalDir​(@Nullable
                                                      Context context,
                                                      @Nullable
                                                      java.lang.String path)
        Returns the default external directory for the app appended with the supplied path.
        Parameters:
        context - The context to be used.
        path - The path to be appended.
        Returns:
        The default external directory for the app appended with the supplied path.
        See Also:
        Context.getExternalFilesDir(String)
      • getTempDir

        @Nullable
        public static java.lang.String getTempDir​(@Nullable
                                                  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.
        See Also:
        getExternalDir(Context, String)
      • getPublicDir

        @Nullable
        public static java.io.File getPublicDir​(@Nullable
                                                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.
        See Also:
        Environment.getExternalStoragePublicDirectory(String)
      • getPublicDir

        @Nullable
        public static java.io.File getPublicDir​(@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.
        See Also:
        getPublicDir(String)
      • 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 exists 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)
        Deletes 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 Uri getUriFromFile​(@Nullable
                                         Context context,
                                         @Nullable
                                         java.io.File file)
        Returns URI from the file.

        It will automatically use the 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​(@Nullable
                                                          Context context,
                                                          @Nullable
                                                          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​(@Nullable
                                          Context context,
                                          @Nullable
                                          Uri sourceUri,
                                          @Nullable
                                          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
                                          Context context,
                                          @Nullable
                                          Uri sourceUri,
                                          @Nullable
                                          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​(@Nullable
                                                Context context,
                                                @Nullable
                                                java.lang.String data,
                                                @Nullable
                                                Uri sourceUri,
                                                @Nullable
                                                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
                                                Context context,
                                                @Nullable
                                                java.lang.String data,
                                                @Nullable
                                                Uri sourceUri,
                                                @Nullable
                                                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
                                                Context context,
                                                @Nullable
                                                java.lang.String data,
                                                @Nullable
                                                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​(@Nullable
                                                          Context context,
                                                          @Nullable
                                                          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 Uri getBitmapUri​(@Nullable
                                       Context context,
                                       @Nullable
                                       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 Uri getBitmapUri​(@Nullable
                                       Context context,
                                       @Nullable
                                       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
                                               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 be used.
        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
                                               Context context,
                                               @Nullable
                                               Uri uri,
                                               @Nullable
                                               java.lang.String extension)
        Checks whether the extension is valid for a URI.
        Parameters:
        context - The context to be used.
        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
                                               Context context,
                                               @Nullable
                                               Intent intent,
                                               @Nullable
                                               java.lang.String extension)
        Checks whether the extension is valid for an intent.
        Parameters:
        context - The context to be used.
        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
                                              Context context,
                                              @Nullable
                                              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
                                              Context context,
                                              @Nullable
                                              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
                                              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
                                     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:
        ActivityNotFoundException - The activity not found exception.
      • shareFiles

        public static void shareFiles​(@NonNull
                                      Activity activity,
                                      @Nullable
                                      java.lang.String title,
                                      @Nullable
                                      java.lang.String subject,
                                      @NonNull
                                      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:
        ActivityNotFoundException - The activity not found exception.
      • getSaveToFileIntent

        @NonNull
        public static Intent getSaveToFileIntent​(@NonNull
                                                 Context context,
                                                 @Nullable
                                                 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 Intent getSaveToFileIntent​(@NonNull
                                                 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 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 to select a file according to the mime type.