未验证 提交 4ba47994 编写于 作者: B Ben Weiss

Cleanup

* Fix AndroidManifest warnings
* Improve formatting of App
* Request access to legacy storage #cleanup
* Move image insertion in separate function
上级 cb6e7a7b
...@@ -25,12 +25,13 @@ ...@@ -25,12 +25,13 @@
<application <application
android:name=".App" android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:allowBackup="true"
android:theme="@style/AppTheme"
android:requestLegacyExternalStorage="true">
<provider <provider
android:name="androidx.work.impl.WorkManagerInitializer" android:name="androidx.work.impl.WorkManagerInitializer"
......
...@@ -31,5 +31,6 @@ class App : Application(), Configuration.Provider { ...@@ -31,5 +31,6 @@ class App : Application(), Configuration.Provider {
override fun getWorkManagerConfiguration() = override fun getWorkManagerConfiguration() =
Configuration.Builder() Configuration.Builder()
.setWorkerFactory(RenameWorkerFactory()) .setWorkerFactory(RenameWorkerFactory())
.setMinimumLoggingLevel(Log.VERBOSE).build() .setMinimumLoggingLevel(Log.VERBOSE)
.build()
} }
...@@ -16,10 +16,12 @@ ...@@ -16,10 +16,12 @@
package com.example.background.workers package com.example.background.workers
import android.content.ContentResolver
import android.content.Context import android.content.Context
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.net.Uri import android.net.Uri
import android.provider.MediaStore import android.provider.MediaStore
import android.provider.MediaStore.Images.Media
import android.util.Log import android.util.Log
import androidx.work.Data import androidx.work.Data
import androidx.work.Worker import androidx.work.Worker
...@@ -38,17 +40,15 @@ class SaveImageToGalleryWorker(appContext: Context, workerParams: WorkerParamete ...@@ -38,17 +40,15 @@ class SaveImageToGalleryWorker(appContext: Context, workerParams: WorkerParamete
override fun doWork(): Result { override fun doWork(): Result {
val resolver = applicationContext.contentResolver val resolver = applicationContext.contentResolver
return try { return try {
val resourceUri = Uri.parse(inputData.getString(Constants.KEY_IMAGE_URI)) val input = Uri.parse(inputData.getString(Constants.KEY_IMAGE_URI))
val bitmap = BitmapFactory.decodeStream(resolver.openInputStream(resourceUri)) val imageLocation = insertImage(resolver, input)
val imageUrl = MediaStore.Images.Media.insertImage( if (imageLocation.isNullOrEmpty()) {
resolver, bitmap, DATE_FORMATTER.format(Date()), TITLE)
if (imageUrl.isEmpty()) {
Log.e(TAG, "Writing to MediaStore failed") Log.e(TAG, "Writing to MediaStore failed")
Result.failure() Result.failure()
} }
// Set the result of the worker by calling setOutputData(). // Set the result of the worker by calling setOutputData().
val output = Data.Builder() val output = Data.Builder()
.putString(Constants.KEY_IMAGE_URI, imageUrl) .putString(Constants.KEY_IMAGE_URI, imageLocation)
.build() .build()
Result.success(output) Result.success(output)
} catch (exception: Exception) { } catch (exception: Exception) {
...@@ -57,6 +57,13 @@ class SaveImageToGalleryWorker(appContext: Context, workerParams: WorkerParamete ...@@ -57,6 +57,13 @@ class SaveImageToGalleryWorker(appContext: Context, workerParams: WorkerParamete
} }
} }
private fun insertImage(resolver: ContentResolver, resourceUri: Uri): String? {
val bitmap = BitmapFactory.decodeStream(resolver.openInputStream(resourceUri))
return Media.insertImage(
resolver, bitmap, DATE_FORMATTER.format(Date()), TITLE
)
}
companion object { companion object {
private const val TAG = "SvImageToGalleryWrkr" private const val TAG = "SvImageToGalleryWrkr"
private const val TITLE = "Filtered Image" private const val TITLE = "Filtered Image"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册