使用WorkManager上传大位图 [英] Uploading large bitmaps using WorkManager

查看:416
本文介绍了使用WorkManager上传大位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用WorkManager将位图上载到服务器.基本上,用户拍照并按下按钮将其上传到服务器.

I'm trying to use WorkManager to upload a bitmap to the server. Basically the user takes a picture and presses a button to upload it to the server.

但是,当我尝试使用Work Manager的Data.Builder类来序列化位图时,就会出现问题,该类的限制为10240字节.因此,如果我执行以下操作:

However, the problem comes when I try to serialise the bitmap using the Data.Builder class of Work Manager, which has a limit of 10240 bytes. Therefore, if I do the following:

val data = Data.Builder()
//Add parameter in Data class. just like bundle. You can also add Boolean and Number in parameter.
data.putString(IMAGE_NAME, identifier)
data.putByteArray(BITMAP_ARRAY, imageBytes) 

将引发以下崩溃java.lang.IllegalStateException: Data cannot occupy more than 10240 bytes when serialized

在启动工作管理器之前,我总是可以将照片保存到文件中,然后在工作管理器中读取文件.但是,如果可能的话,我会避免所有文件管理,因为用户可以始终关闭应用程序等.

I could always save the photo to a file before starting the work manager, and in work manager read the file. However, I would rathe avoid all file management if possible, because the user could always close the app, etc.

如果服务器成功响应,我只是想保存文件.

I just wanted to save the file, if the server responded successful.

还有其他方法可以实现这一目标吗?谷歌对这种事情有建议"吗?

Is there any other way to achieve this? Is there a google "suggestion" for this kind of things?

这是我的doWork() WorkManager功能

Here is my doWork() of WorkManager functionality

    override fun doWork(): Result {
        val identifier = inputData.getString(IMAGE_NAME)!!
        val imageBytes = inputData.getByteArray(BITMAP_ARRAY)!!

        val result = executeRequest(identifier, imageBytes)

        return if (result.isSuccess()) {
            //saving image
            val bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
            saveToInternalStorage(context, identifier, bitmap)
            Result.success()
        } else {
            Result.failure()
        }
    }

推荐答案

如果可能的话,我会避免所有文件管理,因为用户可以始终关闭应用程序等.

I would rathe avoid all file management if possible, because the user could always close the app, etc.

然后,您不应使用WorkManager. 文档明确指出:

Then you should not use WorkManager. The documentation states this clearly:

WorkManager不适用于正在进行的后台工作,如果应用程序消失,则可以安全地终止后台工作

WorkManager is not intended for in-process background work that can safely be terminated if the app process goes away

关于将大量数据放入Data中,文档对此也很清楚:

With regards to putting a lot of data in a Data, the documentation is clear on this as well:

数据对象的最大大小限制为10KB. [...]如果您需要传入和传出更多数据,请将该数据放在其他位置

There is a maximum size limit of 10KB for Data objects. [...] If you need to pass more data in and out of your Worker, you should put your data elsewhere

因此,如果您毕竟想使用WorkManager(这对我来说是个好主意),则必须将大位图放入文件中,将该文件的URI放入Data对象中,然后然后在您的doWork()中从该文件上传位图,然后删除该文件.

So if you want to use WorkManager for this after all (which sounds like a good idea to me), you have to put the large bitmap in a file, put the URI of that file in the Data object, and then in your doWork() upload the bitmap from that file and then delete the file.

如果您在doWork()的中间杀死应用程序,则WorkManager框架将稍后(不增加UI)启动您的应用程序流程(不增加UI),并尝试再次进行上传.

If you kill your app in in the middle of doWork(), the WorkManager framework will start your app process (without a UI) later (with increasing backoff time) and attempt to do the upload again.

这篇关于使用WorkManager上传大位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆