Skip to main content
Language:

Background Export

PhotoEditor SDK supports exporting photos in the background using WorkManager. Background export requires the backend:headless module. Refer to our documentation on how to include the module in your project.

Start Editor#

For the sake of this example, we load a photo from the resources into the photo editor.

Configure the PhotoEditorSaveSettings to set the outputMode to only export the SettingsList (instead of the photo). This SettingsList will be used later to export the photo in the background.

Handle Result#

The result from the editor is received in the onActivityResult() method. EditorSDKResult wraps around the intent and provides a convenient API to check the result. Here, we obtain the SettingsList using EditorSDKResult.settingsList and create a WorkRequest using the DocumentRenderWorker.createWorker() method. This WorkRequest is then enqueued in the WorkManager for background processing.

Note the usage of the use() method on the SettingsList. It automatically releases the SettingsList after executing the block so we don't have to release it manually.

Observe Progress#

Here, we observe progress information of the scheduled background exports using the getWorkInfosByTagLiveData() method.

DocumentRenderWorker uses the DocumentRenderWorker.DEFAULT_WORK_INFO_TAG tag and the progress is obtained using the DocumentRenderWorker.FLOAT_PROGRESS_KEY key.

File:
class SavePhotoInBackground(private val activity: AppCompatActivity) : Example(activity) {
override fun invoke() {
// In this example, we need access to the Uri(s) after the editor is closed
// so we pass true in the constructor
val settingsList = PhotoEditorSettingsList(true)
// Set the source as the Uri of the image to be loaded
.configure<LoadSettings> {
it.source = activity.resourceUri(R.drawable.la)
}
// Export only SettingsList, we use this SettingsList later to export in the background
.configure<PhotoEditorSaveSettings> {
it.outputMode = OutputMode.EXPORT_ONLY_SETTINGS_LIST
}
// Start the photo editor using PhotoEditorBuilder
// The result will be obtained in onActivityResult() corresponding to EDITOR_REQUEST_CODE
PhotoEditorBuilder(activity)
.setSettingsList(settingsList)
.startActivityForResult(activity, EDITOR_REQUEST_CODE)
// Release the SettingsList once done
settingsList.release()
WorkManager.getInstance(activity)
.getWorkInfosByTagLiveData(DocumentRenderWorker.DEFAULT_WORK_INFO_TAG)
.observe(activity) {
it.forEach { job ->
Log.d("IMG.LY", "State: ${job.state} Progress: ${job.progress.getFloat(DocumentRenderWorker.FLOAT_PROGRESS_KEY, 1f)}")
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
super.onActivityResult(requestCode, resultCode, intent)
intent ?: return
if (requestCode == EDITOR_REQUEST_CODE) {
val result = EditorSDKResult(intent)
when (result.resultStatus) {
EditorSDKResult.Status.CANCELED -> showMessage("Editor cancelled")
EditorSDKResult.Status.DONE_WITHOUT_EXPORT -> {
// Export the photo in background using WorkManager
result.settingsList.use { document ->
WorkManager.getInstance(activity).enqueue(DocumentRenderWorker.createWorker(document))
}
}
else -> {
}
}
}
}
}