Class DynamicFileUtils

java.lang.Object
com.pranavpandey.android.dynamic.util.DynamicFileUtils

public class DynamicFileUtils extends 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:
  • Field Details

  • Constructor Details

    • DynamicFileUtils

      public DynamicFileUtils()
  • Method Details

    • getExternalDir

      @Nullable public static String getExternalDir(@Nullable Context context, @Nullable 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:
    • getTempDir

      @Nullable public static 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:
    • getPublicDir

      @Nullable public static File getPublicDir(@Nullable 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:
    • getPublicDir

      @Nullable public static File getPublicDir(@NonNull String type, @Nullable 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:
    • getBaseName

      @Nullable public static String getBaseName(@Nullable 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 String getExtension(@Nullable 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 String getExtension(@Nullable 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 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 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 File dir, @NonNull File zip) throws IOException
      Creates a zip archive from the directory.
      Parameters:
      dir - The directory to be archived.
      zip - The output zip archive.
      Throws:
      IOException - Throws IO exception.
    • unzip

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

      @Nullable public static Uri getUriFromFile(@Nullable Context context, @Nullable 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:
    • getFileFromUri

      @Nullable public static File getFileFromUri(@Nullable Uri uri)
      Returns file from the URI.
      Parameters:
      uri - The URI to get the file.
      Returns:
      The file from the URI.
      See Also:
    • getFileFromUri

      @Nullable public static File getFileFromUri(@Nullable String uri)
      Returns file from the URI string.
      Parameters:
      uri - The URI to get the file.
      Returns:
      The file from the URI string.
      See Also:
    • getFileNameFromUri

      @Nullable public static 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:
    • writeToFile

      public static boolean writeToFile(@NonNull File source, @NonNull File destination, @NonNull 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 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:
    • writeStringToFile

      public static boolean writeStringToFile(@Nullable Context context, @Nullable String data, @Nullable Uri sourceUri, @Nullable Uri destinationUri, @NonNull 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 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

      public static boolean writeStringToFile(@NonNull Context context, @Nullable 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:
    • readStringFromFile

      @Nullable public static String readStringFromFile(@Nullable Context context, @Nullable Uri fileUri, boolean nextLine)
      Reads a string data from the file URI.
      Parameters:
      context - The context to get content resolver.
      fileUri - The source file URI.
      nextLine - true to append next line character after each line.
      Returns:
      The string data after reading the file.
      See Also:
    • readStringFromFile

      @Nullable public static 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.
      See Also:
    • getBitmapUri

      @Nullable public static Uri getBitmapUri(@Nullable Context context, @Nullable Bitmap bitmap, @NonNull String name, @Nullable 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:
    • getBitmapUri

      @Nullable public static Uri getBitmapUri(@Nullable Context context, @Nullable Bitmap bitmap, @NonNull 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:
    • isValidExtension

      public static boolean isValidExtension(@Nullable String path, @Nullable 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 File file, @Nullable 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 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 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 String mimeType, @Nullable 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 String mimeType, @Nullable 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 File file, @NonNull String mimeType, @Nullable 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 String title, @Nullable String subject, @NonNull File file, @NonNull 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 String title, @Nullable String subject, @NonNull Uri[] uris, @Nullable 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 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 File file, @NonNull 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 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.
    • takePersistedUriPermission

      public static void takePersistedUriPermission(@Nullable Context context, @Nullable Intent data)
      Try to take the persistable storage permission for the intent data URI.
      Parameters:
      context - The context to get the content resolver.
      data - The intent to get the file URI.
      See Also: