Skip to main content
Language:

Serialization

PhotoEditor SDK supports serialization and deserialization, allowing your users to save and revise their work at any time. Serialization requires the backend:serializer 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 for serialization. You can choose to skip this step and both the photo as well as the SettingsList will be exported.

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 launch a coroutine using Dispatchers.IO. Inside the coroutine, we create a file, obtain the SettingsList (via EditorSDKResult.settingsList), and serialize it using the IMGLYFileWriter.writeJson() method. You can optionally serialize the SettingsList into a String (using IMGLYFileWriter.writeJsonAsString()) or a ByteArray (using IMGLYFileWriter.writeJsonAsBytes()).

For details on the JSON structure you can download our schema.

For further processing, you can upload the serialization to a remote server (compare Save Photo To Remote URL) to make the editor state available on other devices as well.

The next section explains how to restore the editor state by deserializing these settings.

File:
class PhotoSerialization(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 for serialization
// You can skip this if you want to export the photo as well
.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()
}
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 -> {
activity.lifecycleScope.launch(Dispatchers.IO) {
result.settingsList.use { settingsList ->
try {
val file = File(activity.getExternalFilesDir(null), "pesdk.json")
if (file.exists()) {
file.delete()
}
file.createNewFile()
// Serialize the settingsList as JSON into the file
IMGLYFileWriter(settingsList).writeJson(file)
withContext(Dispatchers.Main) {
showMessage("Serialisation saved successfully")
}
} catch (e: IOException) {
e.printStackTrace()
withContext(Dispatchers.Main) {
showMessage("Error saving serialisation")
}
}
}
}
}
else -> {
}
}
}
}
}