Class DynamicFileUtils
java.lang.Object
com.pranavpandey.android.dynamic.util.DynamicFileUtils
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 Summary
Modifier and TypeFieldDescriptionstatic final String
Constant for the next line character.static final String
Constant for the default data directory.static final String
Constant for the default temp directory.static final String
Default file extension for the image.static final String
Default suffix for the file provider.static final String
Constant for theany
mime type.static final String
Constant for theapplication/*
mime type.static final String
Constant for theapplication/octet-stream
mime type.static final String
Constant to match the content URI.static final String
Constant to match the file URI. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
deleteDirectory
(File dir) Deletes a directory.static String
getBaseName
(String fileName) Returns the base name without extension of given file name.static Uri
getBitmapUri
(Context context, Bitmap bitmap, String name) Save and returns URI from the bitmap.static Uri
getBitmapUri
(Context context, Bitmap bitmap, String name, String extension) Save and returns URI from the bitmap.static String
getExtension
(File file) Returns the extension of a file.static String
getExtension
(String fileName) Returns the extension of a file name.static String
getExternalDir
(Context context, String path) Returns the default external directory for the app appended with the supplied path.static File
getFileFromUri
(Uri uri) Returns file from the URI.static File
getFileFromUri
(String uri) Returns file from the URI string.static String
getFileNameFromUri
(Context context, Uri uri) Returns file name from the URI.static Intent
getFileSelectIntent
(String mimeType) Returns an intent to select a file according to the mime type.static File
getPublicDir
(String type) Returns the default directory according to the supplied type if accessible.static File
getPublicDir
(String type, String fileName) Returns the default directory according to the supplied type if accessible.static Intent
getSaveToFileIntent
(Context context, Uri file, String mimeType) Returns an intent to request a storage location for the supplied file.static Intent
getSaveToFileIntent
(Context context, File file, String mimeType) Returns an intent to request a storage location for the supplied file.static String
getTempDir
(Context context) Returns the defaulttemp
directory for the context.static Uri
getUriFromFile
(Context context, File file) Returns URI from the file.static boolean
isValidExtension
(Context context, Intent intent, String extension) Checks whether the extension is valid for an intent.static boolean
isValidExtension
(Context context, Uri uri, String extension) Checks whether the extension is valid for a URI.static boolean
isValidExtension
(Context context, File file, String extension) Checks whether the extension is valid for a file.static boolean
isValidExtension
(String path, String extension) Checks whether the extension is valid for a path.static boolean
isValidMimeType
(Context context, Intent intent, String mimeType, String extension) Checks whether the mime type is valid for an intent data.static boolean
isValidMimeType
(Context context, Uri uri, String mimeType, String extension) Checks whether the mime type is valid for a URI.static boolean
isValidMimeType
(Context context, File file, String mimeType, String extension) Checks whether the mime type is valid for a file.static String
readStringFromFile
(Context context, Uri fileUri) Reads a string data from the file URI.static String
readStringFromFile
(Context context, Uri fileUri, boolean nextLine) Reads a string data from the file URI.static void
Share file according to the mime type.static void
Share multiple files.static void
takePersistedUriPermission
(Context context, Intent data) Try to take the persistable storage permission for the intent data URI.static void
Extracts a zip archive.static boolean
verifyFile
(File file) Verifies a file if it exists or not.static boolean
writeStringToFile
(Context context, String data, Uri sourceUri) Writes a string data to file URI.static boolean
writeStringToFile
(Context context, 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
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, String mode) Writes a file URI from the source to destination.static boolean
writeToFile
(File source, File destination, String outputFileName) Writes a file from the source to destination.static void
zipDirectory
(File dir, File zip) Creates a zip archive from the directory.
-
Field Details
-
MIME_ALL
Constant for theany
mime type.- See Also:
-
MIME_APPLICATION
Constant for theapplication/*
mime type.- See Also:
-
MIME_OCTET_STREAM
Constant for theapplication/octet-stream
mime type.- See Also:
-
FILE_PROVIDER
Default suffix for the file provider.- See Also:
-
URI_MATCHER_CONTENT
Constant to match the content URI.- See Also:
-
URI_MATCHER_FILE
Constant to match the file URI.- See Also:
-
DIR_DATA
Constant for the default data directory.- See Also:
-
DIR_TEMP
Constant for the default temp directory.- See Also:
-
EXTENSION_IMAGE
Default file extension for the image.- See Also:
-
CHARACTER_NEXT_LINE
Constant for the next line character.- See Also:
-
-
Constructor Details
-
DynamicFileUtils
public DynamicFileUtils()
-
-
Method Details
-
getExternalDir
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
Returns the defaulttemp
directory for the context.- Parameters:
context
- The context to get the package name.- Returns:
- The default
temp
directory for the context. - See Also:
-
getPublicDir
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
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
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
Returns the extension of a file name.- Parameters:
fileName
- The file name to retrieve its extension.- Returns:
- The extension of the file name.
-
getExtension
Returns the extension of a file.- Parameters:
file
- The file to retrieve its extension.- Returns:
- The extension of the file.
-
verifyFile
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
Deletes a directory.- Parameters:
dir
- The directory to be deleted.- Returns:
true
if the directory has been deleted successfully.
-
zipDirectory
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
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
Returns file from the URI.- Parameters:
uri
- The URI to get the file.- Returns:
- The file from the URI.
- See Also:
-
getFileFromUri
Returns file from the URI string.- Parameters:
uri
- The URI to get the file.- Returns:
- The file from the URI string.
- See Also:
-
getFileNameFromUri
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
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
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.
-
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
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
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:
-