Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

Commit 4ba4799

Browse files
Cleanup
* Fix AndroidManifest warnings * Improve formatting of App * Request access to legacy storage #cleanup * Move image insertion in separate function
1 parent cb6e7a7 commit 4ba4799

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

WorkManagerSample/app/src/main/AndroidManifest.xml

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525

2626
<application
2727
android:name=".App"
28-
android:allowBackup="true"
2928
android:icon="@mipmap/ic_launcher"
3029
android:label="@string/app_name"
3130
android:roundIcon="@mipmap/ic_launcher_round"
3231
android:supportsRtl="true"
33-
android:theme="@style/AppTheme">
32+
android:allowBackup="true"
33+
android:theme="@style/AppTheme"
34+
android:requestLegacyExternalStorage="true">
3435

3536
<provider
3637
android:name="androidx.work.impl.WorkManagerInitializer"

WorkManagerSample/app/src/main/java/com/example/background/App.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ class App : Application(), Configuration.Provider {
3131
override fun getWorkManagerConfiguration() =
3232
Configuration.Builder()
3333
.setWorkerFactory(RenameWorkerFactory())
34-
.setMinimumLoggingLevel(Log.VERBOSE).build()
34+
.setMinimumLoggingLevel(Log.VERBOSE)
35+
.build()
3536
}

WorkManagerSample/lib/src/main/java/com/example/background/workers/SaveImageToGalleryWorker.kt

+13-6
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
package com.example.background.workers
1818

19+
import android.content.ContentResolver
1920
import android.content.Context
2021
import android.graphics.BitmapFactory
2122
import android.net.Uri
2223
import android.provider.MediaStore
24+
import android.provider.MediaStore.Images.Media
2325
import android.util.Log
2426
import androidx.work.Data
2527
import androidx.work.Worker
@@ -38,17 +40,15 @@ class SaveImageToGalleryWorker(appContext: Context, workerParams: WorkerParamete
3840
override fun doWork(): Result {
3941
val resolver = applicationContext.contentResolver
4042
return try {
41-
val resourceUri = Uri.parse(inputData.getString(Constants.KEY_IMAGE_URI))
42-
val bitmap = BitmapFactory.decodeStream(resolver.openInputStream(resourceUri))
43-
val imageUrl = MediaStore.Images.Media.insertImage(
44-
resolver, bitmap, DATE_FORMATTER.format(Date()), TITLE)
45-
if (imageUrl.isEmpty()) {
43+
val input = Uri.parse(inputData.getString(Constants.KEY_IMAGE_URI))
44+
val imageLocation = insertImage(resolver, input)
45+
if (imageLocation.isNullOrEmpty()) {
4646
Log.e(TAG, "Writing to MediaStore failed")
4747
Result.failure()
4848
}
4949
// Set the result of the worker by calling setOutputData().
5050
val output = Data.Builder()
51-
.putString(Constants.KEY_IMAGE_URI, imageUrl)
51+
.putString(Constants.KEY_IMAGE_URI, imageLocation)
5252
.build()
5353
Result.success(output)
5454
} catch (exception: Exception) {
@@ -57,6 +57,13 @@ class SaveImageToGalleryWorker(appContext: Context, workerParams: WorkerParamete
5757
}
5858
}
5959

60+
private fun insertImage(resolver: ContentResolver, resourceUri: Uri): String? {
61+
val bitmap = BitmapFactory.decodeStream(resolver.openInputStream(resourceUri))
62+
return Media.insertImage(
63+
resolver, bitmap, DATE_FORMATTER.format(Date()), TITLE
64+
)
65+
}
66+
6067
companion object {
6168
private const val TAG = "SvImageToGalleryWrkr"
6269
private const val TITLE = "Filtered Image"

0 commit comments

Comments
 (0)