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