Android file writing helper library for API Degree 21+
Gradle
// If you wish to deal with the runtime permission requesting from the library
implementation ‘com.akexorcist.filewritercompat:permission:1.1.0‘
Write information to those directories: Inner app-specific file (/information/information/<package_name>/information/) Inner app-specific cache (/information/information/<package_name>/cache/) Exterior app-specific file (/<external_storage>/Android/information/<package_name>/information/) Exterior app-specific cache (/<external_storage>/Android/information/<package_name>/cache/) Exterior shareable file (/<external_storage>/<directory_type>/) Help for API degree 21 – 33 Auto-create subdirectories Constructed-in permission dealing with (extra dependency is required) Auto media scanner triggers when saving a file in an exterior shareable listing
Frequent utilization
exercise: FragmentActivity,
information: ByteArray,
filename: String
) {
val author = FileWriterCompat.Builder.createInternalAppSpecificFile(
fileNameWithExtension = filename,
)
// (Elective) Auto-create sub listing if not exist
// or skip this technique to retailer the file in guardian listing
.setChildPath(“pattern/exported“)
.construct()
val end result: FileResult<Uri, InternalAppSpecificFile.ErrorReason> = author.write(exercise, information)
}
All supported directories
val executor: InternalAppSpecificFile.Executor = FileWriterCompat.Builder
.createInternalAppSpecificFile(/* … */)
.construct()
val end result: FileRequest<Uri, InternalAppSpecificFile.ErrorReason> = executor.write(/* … */)
// Inner app-specific cache
val executor: InternalAppSpecificCache.Executor = FileWriterCompat.Builder
.createInternalAppSpecificCache(/* … */)
.construct()
val end result: FileRequest<Uri, InternalAppSpecificCache.ErrorReason> = executor.write(/* … */)
// Exterior app-specific file
val executor: ExternalAppSpecificFile.Executor = FileWriterCompat.Builder
.createExternalAppSpecificFile(/* … */)
.construct()
val end result: FileRequest<Uri, ExternalAppSpecificFile.ErrorReason> = executor.write(/* … */)
// Exterior app-specific cache
val executor: ExternalAppSpecificCache.Executor = FileWriterCompat.Builder
.createExternalAppSpecificCache(/* … */)
.construct()
val end result: FileRequest<Uri, ExternalAppSpecificCache.ErrorReason> = executor.write(/* … */)
// Exterior shareable file
val executor: ExternalShareableFile.Executor = FileWriterCompat.Builder
.createExternalShareableFile(/* … */)
.construct()
val end result: FileRequest<Uri, ExternalShareableFile.ErrorReason> = executor.write(/* … */)
Inner app-specific file’s parameter
Requires
Filename with extension (String) – e.g, “picture.jpg”
Elective
Baby path (String) – e.g, “pattern/exported”
Inner app-specific cache’s parameter
Requires
Filename with extension (String) – e.g, “picture.jpg”
Elective
Baby path (String) – e.g, “pattern/exported”
Exterior app-specific file’s parameter
Requires
Filename with extension (String) – e.g, “picture.jpg”
Elective
Listing kind (String) – e.g, Atmosphere.DIRECTORY_PICTURES Baby path (String) – e.g, “pattern/exported”
Exterior app-specific cache’s parameter
Requires
Filename with extension (String) – e.g, “picture.jpg”
Elective
Baby path (String) – e.g, “pattern/exported”
Exterior shareable file’s parameter
Requires
Listing kind (String) – e.g, Atmosphere.DIRECTORY_PICTURES Filename with extension (String) – e.g, “picture.jpg”
Elective
Baby path (String) – e.g, “pattern/exported” Storage permission request handing (StoragePermissionRequest) – e.g, NoOperationStoragePermissionRequest
Write exterior storage permission in exterior shareable file
The Manifest.permission.WRITE_EXTERNAL_STORAGE_PERMISSION have to be granted when working on Android 10 or decrease. No permission is required for Android 11 or increased.
There are two options to deal with the runtime permission requesting on this library:
Name the runtime permission requesting earlier than calling this library when working on Android 10 or decrease. Create a customized StoragePermissionRequest and inject it in ExternalShareableFile.Builder.
val executor: ExternalShareableFile.Executor = FileWriterCompat.Builder.createExternalShareableFile(/* … */)
.setStoragePermissionRequest(customStoragePermissionRequest)
.construct()
There are two built-in storage permission requests relying on what you need:
NoOperationStoragePermissionRequest – No operation, simply verify the permission. PekoStoragePermissionRequest – Request the permission with Peko library com.akexorcist.filewritercompat:permission:<latest_version is required.
You may customise this operation by your individual:
override droop enjoyable requestWriteExternalStoragePermission(exercise: FragmentActivity): Boolean {
/* Any operation with boolean end result */
}
}
Knowledge kind
There are 4 overloaded strategies for the write technique:
droop enjoyable write(exercise: FragmentActivity, information: String): FileResult<Uri, ErrorReaon>
droop enjoyable write(exercise: FragmentActivity, information: Any): FileResult<Uri, ErrorReaon>
droop enjoyable <DATA> write(exercise: FragmentActivity, information: DATA, author: (DATA, File) -> Unit): FileResult<Uri, ErrorReaon>
Moreover, you’ll be able to create your customized information author perform for any information kind.
For instance:
if (!file.exists()) {
file.createNewFile()
}
val fos = FileOutputStream(file)
ObjectOutputStream(fos).use {
it.writeInt(information)
}
}
val information: Int = 1024
val exercise: FragmentActivity
val executor: WriterExecutor<Uri, ErrorReason> = /* … */
val end result: FileResult<Uri, ErrorReason> = executor.write(exercise, information, customWriter)
Error dealing with
When the write technique is known as, the end result from this technique shall be FileResult<Uri, ErrorReason>.
The ErrorReason depends upon what sort of listing you’re utilizing. For instance, the end result shall be FileResult<Uri, ExternalShareableFile.ErrorReason> while you create the file author with ExternalShareableFile.Builder.
val end result: FileResult<Uri, ExternalShareableFile.ErrorReason> = executor.write(/* … */)
when (end result) {
is FileResult.Success<Uri> -> {
/* File was saved */
val uri: Uri = end result.end result
}
is FileResult.Error<ExternalShareableFile.ErrorReason> -> {
/* File was not saved for some cause */
when (end result.cause) {
is ExternalShareableFile.ErrorReason.WriteExternalStoragePermissionDenied -> {
/* WRITER_EXTERNAL_STORAGE permission was denied */
}
is ExternalShareableFile.ErrorReason.InvalidDirectoryType -> {
/* Invalid listing kind parameter */
}
/* … */
}
}
}
Every kind of listing has a distinct error cause.
Troubleshooting
Minimal SDK model can’t be smaller than model 23 declared in library [com.markodevcic:peko:<version>]
This may occur while you use com.akexorcist.filewritercompat:permission, because the Peko library has a minimal SDK model at 23.
To resolve this downside, declare the <uses-sdk> tag in your Android manifest to power utilization:
<uses-sdk instruments:overrideLibrary=“com.markodevcic.peko“/>
<!– … –>
</software>
Copyright 2023 Akexorcist
Licensed underneath the Apache License, Model 2.0 (the “License”); chances are you’ll not use this work besides in compliance with the License. It’s possible you’ll acquire a duplicate of the License within the LICENSE file, or at:
http://www.apache.org/licenses/LICENSE-2.0
Until required by relevant legislation or agreed to in writing, software program distributed underneath the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, both categorical or implied. See the License for the precise language governing permissions and limitations underneath the License.