Serialization
Our serialization functionality empowers you to save the current settings of the changes and recover it the next time the editor is opened again. The settings will be stored in a plain JSON file. For details on the JSON structure, you can download our schema.
Saving the settings#
When the editor is closed, the SettingsList
is parceled into the android.content.Intent
data of onActivityResult(int requestCode, int resultCode, android.content.Intent data)
You can parse the serialize and write settings by calling the writeJson(java.io.File)
method on a fresh PESDKFileWriter
object.
Here is some example code to get you started:
protected void onActivityResult(int requestCode, int resultCode, android.content.Intent data) {super.onActivityResult(requestCode, resultCode, data);int EDITOR_RESULT = 2;if (resultCode == RESULT_OK && requestCode == EDITOR_RESULT) {SettingsList settingsList = data.getParcelableExtra(ImgLyIntent.SETTINGS_LIST);IMGLYFileWriter writer = new IMGLYFileWriter(settingsList);// TODO: Choose a better file pathFile file = new File(Environment.getExternalStorageDirectory(), "staveState.pesdk");try {if (file.exists()) {file.delete();}file.createNewFile();writer.writeJson(file);Trace.out("JSON", "json written");} catch (IOException e) {e.printStackTrace();Trace.out("JSON", "error on write json");}}//...}
Restoring the settings#
To set the initial editor settings, load the saved settings with a fresh PESDKFileReader
object. This has to be done before the editor is presented. Here is an example, to demonstrate the process:
PhotoEditorSettingsList settingsList = createInitialPesdkSettingsList();int PESDK_RESULT = 1;// Set input imagesettingsList.getSettingsModel(LoadSettings.class).setSource(inputImage);// TODO: Choose a better file pathFile file = new File(Environment.getExternalStorageDirectory(), "staveState.pesdk");if (file.exists()) {IMGLYFileReader reader = new IMGLYFileReader(settingsList);try {reader.readJson(file);} catch (IOException e) {Toast.makeText(this, "Error while opening json.", Toast.LENGTH_LONG).show();e.printStackTrace();return;}} else {Toast.makeText(this, "No save state found.", Toast.LENGTH_LONG).show();return;}new PhotoEditorBuilder(this).setSettingsList(settingsList).startActivityForResult(this, PESDK_RESULT);