Skip to main content
Language:

Deserialization

PhotoEditor SDK supports serialization and deserialization, allowing your users to save and revise their work at any time. Deserialization requires the backend:serializer module. Refer to our documentation on how to include the module in your project.

Restoring a settings file#

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

To restore an editor instance to a previous state, we read the serialization from a JSON file using the IMGLYFileReader.readJson() method. Since reading a file is an IO Operation, we do this inside of a coroutine on the IO Dispatcher.

The IMGLYFileReader.readJson() method is overloaded to also accept a String or a ByteArray to read into a SettingsList.

File:
class PhotoDeserialization(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(false)
// Set the source as the Uri of the image to be loaded
.configure<LoadSettings> {
it.source = activity.resourceUri(R.drawable.la)
}
activity.lifecycleScope.launch {
withContext(Dispatchers.IO) {
val file = File(activity.getExternalFilesDir(null), "pesdk.json")
if (file.exists()) {
try {
// Deserialize JSON file and read it into SettingsList
IMGLYFileReader(settingsList).readJson(file)
} catch (e: IOException) {
withContext(Dispatchers.Main) {
showMessage("Error reading serialisation")
}
}
} else {
withContext(Dispatchers.Main) {
showMessage("Serialisation file not found")
}
}
}
// 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.EXPORT_DONE -> showMessage("Result saved at ${result.resultUri}")
else -> {
}
}
}
}