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.
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)
The following crash will be thrown 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?
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()
}
}