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
FieldsModifier and TypeFieldDescriptionstatic final StringConstant for the next line character.static final StringConstant for the default data directory.static final StringConstant for the default temp directory.static final StringDefault file extension for the image.static final StringDefault suffix for the file provider.static final StringConstant for theanymime type.static final StringConstant for theapplication/*mime type.static final StringConstant for theapplication/octet-streammime type.static final StringConstant to match the content URI.static final StringConstant to match the file URI. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleandeleteDirectory(File dir) Deletes a directory.static StringgetBaseName(String fileName) Returns the base name without extension of given file name.static UrigetBitmapUri(Context context, Bitmap bitmap, String name) Save and returns URI from the bitmap.static UrigetBitmapUri(Context context, Bitmap bitmap, String name, String extension) Save and returns URI from the bitmap.static StringgetExtension(File file) Returns the extension of a file.static StringgetExtension(String fileName) Returns the extension of a file name.static StringgetExternalDir(Context context, String path) Returns the default external directory for the app appended with the supplied path.static FilegetFileFromUri(Uri uri) Returns file from the URI.static FilegetFileFromUri(String uri) Returns file from the URI string.static StringgetFileNameFromUri(Context context, Uri uri) Returns file name from the URI.static IntentgetFileSelectIntent(String mimeType) Returns an intent to select a file according to the mime type.static FilegetPublicDir(String type) Returns the default directory according to the supplied type if accessible.static FilegetPublicDir(String type, String fileName) Returns the default directory according to the supplied type if accessible.static IntentgetSaveToFileIntent(Context context, Uri file, String mimeType) Returns an intent to request a storage location for the supplied file.static IntentgetSaveToFileIntent(Context context, File file, String mimeType) Returns an intent to request a storage location for the supplied file.static StringgetTempDir(Context context) Returns the defaulttempdirectory for the context.static UrigetUriFromFile(Context context, File file) Returns URI from the file.static booleanisValidExtension(Context context, Intent intent, String extension) Checks whether the extension is valid for an intent.static booleanisValidExtension(Context context, Uri uri, String extension) Checks whether the extension is valid for a URI.static booleanisValidExtension(Context context, File file, String extension) Checks whether the extension is valid for a file.static booleanisValidExtension(String path, String extension) Checks whether the extension is valid for a path.static booleanisValidMimeType(Context context, Intent intent, String mimeType, String extension) Checks whether the mime type is valid for an intent data.static booleanisValidMimeType(Context context, Uri uri, String mimeType, String extension) Checks whether the mime type is valid for a URI.static booleanisValidMimeType(Context context, File file, String mimeType, String extension) Checks whether the mime type is valid for a file.static StringreadStringFromFile(Context context, Uri fileUri) Reads a string data from the file URI.static StringreadStringFromFile(Context context, Uri fileUri, boolean nextLine) Reads a string data from the file URI.static voidShare file according to the mime type.static voidShare multiple files.static voidtakePersistedUriPermission(Context context, Intent data) Try to take the persistable storage permission for the intent data URI.static voidExtracts a zip archive.static booleanverifyFile(File file) Verifies a file if it exists or not.static booleanwriteStringToFile(Context context, String data, Uri sourceUri) Writes a string data to file URI.static booleanwriteStringToFile(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 booleanWrites a string data to file URI from the source to destination.static booleanwriteToFile(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 booleanwriteToFile(Context context, Uri sourceUri, Uri destinationUri, String mode) Writes a file URI from the source to destination.static booleanwriteToFile(File source, File destination, String outputFileName) Writes a file from the source to destination.static voidzipDirectory(File dir, File zip) Creates a zip archive from the directory.
-
Field Details
-
MIME_ALL
Constant for theanymime type.- See Also:
-
MIME_APPLICATION
Constant for theapplication/*mime type.- See Also:
-
MIME_OCTET_STREAM
Constant for theapplication/octet-streammime 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 defaulttempdirectory for the context.- Parameters:
context- The context to get the package name.- Returns:
- The default
tempdirectory 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:
trueif a file can be accessed by automatically creating the sub directories.
-
deleteDirectory
Deletes a directory.- Parameters:
dir- The directory to be deleted.- Returns:
trueif 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
FileProvideron 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:
trueif 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:
trueif 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:
trueif 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:
trueif 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:
trueif 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:
trueif 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-trueto 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_STORAGEpermission 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_STORAGEpermission 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:
trueif 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:
trueif 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:
trueif 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:
trueif 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:
trueif 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:
trueif 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:
trueif 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:
-