}
val resolver = appContext.contentResolver
val picture = BitmapFactory.decodeStream(
resolver.openInputStream(Uri.parse(resourceUri)))
val output = blurBitmap(picture, appContext)
val outputUri = writeBitmapToFile(appContext, output)
val outputData = workDataOf(KEY_IMAGE_URI to outputUri.toString())
Result.success(outputData)
} catch (throwable: Throwable) {
Result.failure()
}
}
}
// 存入图库(别忘了加动态权限)
class SaveImageToFileWorker(
ctx: Context,
params: WorkerParameters
) : Worker(ctx, params) {
private val Title = “Blurred Image”
private val dateFormatter = SimpleDateFormat(
“yyyy.MM.dd ‘at’ HH:mm:ss z”,
Locale.getDefault()
)
override fun doWork(): Result {
makeStatusNotification(“Saving image”, applicationContext)
val resolver = applicationContext.contentResolver
return try {
val resourceUri = inputData.getString(KEY_IMAGE_URI)
val bitmap = BitmapFactory.decodeStream(
resolver.openInputStream(Uri.parse(resourceUri))
)
val imageUrl = MediaStore.Images.Media.insertImage(
resolver, bitmap, Title, dateFormatter.format(Date())
)
if (!imageUrl.isNullOrEmpty()) {
val output = workDataOf(KEY_IMAGE_URI to imageUrl)
Result.success(output)
} else {
Result.failure()
}
} catch (exception: Exception) {
Result.failure()
}
}
}
- 创建执行链
fun applyBlur() {
var continuation = workManager
.beginUniqueWork(
IMAGE_MANIPULATION_WORK_NAME,
ExistingWorkPolicy.REPLACE,
OneTimeWorkRequest
.from(CleanupWorker::class.java)
)
// 反复模糊,这几我就循环三次
for (i in 0 until 3) {
val blurBuilder = OneTimeWorkRequestBuilder()
if (i == 0) {
blurBuilder.setInputData(createInputDataForUri())
}
continuation = continuation.then(blurBuilder.build())
}
val save = OneTimeWorkRequestBuilder()
.addTag(TAG_OUTPUT)
.build()
continuation = continuation.then(save)
continuation.enqueue()
}
相关知识点
知识点一:
WorkManager
根据不同的API使用底层作业来调度服务
知识点二: 工作状态监听
WorkManage
有getWorkInfosXxx
方法获取WorkInfo
,根据WorkInfo
判断工作状态
// MainViewModel
@HiltViewModel
class MainViewModel @Inject constructor(
private val repository: WordRepository,
private val workManager: WorkManager
) : ViewModel() {
private var _imageUri = MutableLiveData() //传入
private var _imageUriOut = MutableLiveData() //传出
internal var outputWorkInfos: LiveData<List> = MutableLiveData()
init {
outputWorkInfos = workManager.getWorkInfosByTagLiveData(TAG_OUTPUT)
}
val imageUri: LiveData<Uri?>
get() = _imageUri
val imageUriOut: LiveData<Uri?>
get() = _imageUriOut
@SuppressLint(“EnqueueWork”)
fun applyBlur() {
var continuation = workManager
.beginUniqueWork(
IMAGE_MANIPULATION_WORK_NAME,
ExistingWorkPolicy.REPLACE,
OneTimeWorkRequest
.from(CleanupWorker::class.java)
)
// 反复模糊,这几我就循环三次
for (i in 0 until 3) {
val blurBuilder = OneTimeWorkRequestBuilder()
if (i == 0) {
blurBuilder.setInputData(createInputDataForUri())
}
continuation = continuation.then(blurBuilder.build())
}
val save = OneTimeWorkRequestBuilder()
.addTag(TAG_OUTPUT)
.build()
continuation = continuation.then(save)
continuation.enqueue()
}
private fun createInputDataForUri(): Data {
val builder = Data.Builder()
imageUri.let {
builder.putString(KEY_IMAGE_URI, imageUri.value.toString())
}
return builder.build()
}
fun setImageUri(uri: Uri) {
_imageUri.postValue(uri)
}
fun setOutputUri(uri: Uri) {
_imageUriOut.postValue(uri)
}
}
// FlowStepTwoFragment.kt
observe(viewModel.outputWorkInfos, ::workInfosUpdate)
private fun workInfosUpdate(list: List) {
if (list.isNullOrEmpty()) {
return
}
val workInfo = list[0]
if (workInfo.state.isFinished) {
showWorkFinished()
val outputImageUri = workInfo.outputData.getString(KEY_IMAGE_URI)
if (!outputImageUri.isNullOrEmpty()) {
viewModel.setOutputUri(Uri.parse(outputImageUri))
bindingTwo.btnCheck.visibility = View.VISIBLE
}
} else {
showWorkInProgress()
}
}
相关链接
Jetpack系列(三) — LiveData
Jetpack系列(四) — ViewModel
Jetpack系列(五) — Room
Jetpack系列(六) — Paging3
Jetpack系列(七) — Hilt
Jetpack系列(八) — Data Bidnding && View Binding
参考资料
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
最后
考虑到文章的篇幅问题,我把这些问题和答案以及我多年面试所遇到的问题和一些面试资料做成了PDF文档,如果有需要的朋友可以私信我【面试】免费领取
篇幅问题,我把这些问题和答案以及我多年面试所遇到的问题和一些面试资料做成了PDF文档,如果有需要的朋友可以私信我【面试】免费领取
[外链图片转存中…(img-IxXTuInm-1710666301565)]
[外链图片转存中…(img-rs0k6fe8-1710666301565)]
喜欢的朋友可以关注、转发、点赞 感谢!