The CreativeEngine allows you to save scenes in a binary format to share them between editors or store them for later editing.
Saving a scene can be done as a either scene file or as an archive file. A scene file does not include any fonts or images. Only the source URIs of assets, the general layout, and element properties are stored. When loading scenes in a new environment, ensure previously used asset URIs are available. Conversely, an archive file contains within it the scene’s assets and references them as relative URIs.
Save Scenes to an Archive
In this example, we will show you how to save scenes as an archive with the CreativeEditor SDK.
As an archive, the resulting Blob
includes all pages and any hidden elements and all the asset data.
To get hold of such a Blob
, you need to use engine.scene.saveToArchive()
.
This is an asynchronous method.
After waiting for the coroutine to finish, we receive a Blob
holding the entire scene currently loaded in the editor including its assets’ data.
val savedSceneString = engine.scene.saveToString(scene = scene)
That Blob
can then be treated as a form file parameter and sent to a remote location.
runCatching { withContext(Dispatchers.IO) { val connection = URL(uploadUrl).openConnection() as HttpURLConnection connection.requestMethod = "POST" connection.doOutput = true connection.outputStream.use { it.write(blob) } connection.connect() }}
Full Code
Here’s the full code:
import android.net.Uriimport kotlinx.coroutines.CoroutineScopeimport kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.launchimport kotlinx.coroutines.withContextimport ly.img.engine.Engineimport java.net.HttpURLConnectionimport java.net.URLimport java.nio.channels.Channels
fun saveSceneToArchive( license: String, userId: String,) = CoroutineScope(Dispatchers.Main).launch { val engine = Engine.getInstance(id = "ly.img.engine.example") engine.start(license = license, userId = userId) engine.bindOffscreen(width = 100, height = 100)
val sceneUri = Uri.parse( "https://cdn.img.ly/assets/demo/v1/ly.img.template/templates/cesdk_postcard_1.scene", ) val scene = engine.scene.load(sceneUri = sceneUri)
val blob = engine.scene.saveToArchive(scene = scene)
withContext(Dispatchers.IO) { val connection = URL("https://example.com/upload/").openConnection() as HttpURLConnection connection.requestMethod = "POST" connection.doOutput = true connection.outputStream.use { Channels.newChannel(it).write(blob) } connection.connect() }
engine.stop()}
Save Scenes to a Blob
In this example, we will show you how to save scenes as a Blob
with the CreativeEditor SDK.
This is done by converting the contents of a scene to a string, which can then be stored or transferred.
For sending these to a remote location, we wrap them in a Blob
and treat it as a file object.
To get hold of the scene contents as string, you need to use engine.scene.saveToString()
.
This is an asynchronous method.
After waiting for the coroutine to finish, we receive a plain string holding the entire scene currently loaded in the editor.
This includes all pages and any hidden elements but none of the actual asset data.
val savedSceneString = engine.scene.saveToString(scene = scene)
The returned string consists solely of ASCII characters and can safely be used further or written to a database.
val blob = savedSceneString.toByteArray(Charsets.UTF_8)
That object can then be treated as a form file parameter and sent to a remote location.
runCatching { withContext(Dispatchers.IO) { val connection = URL(uploadUrl).openConnection() as HttpURLConnection connection.requestMethod = "POST" connection.doOutput = true connection.outputStream.use { it.write(blob) } connection.connect() }}
Full Code
Here’s the full code:
import android.net.Uriimport kotlinx.coroutines.CoroutineScopeimport kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.launchimport kotlinx.coroutines.withContextimport ly.img.engine.Engineimport java.net.HttpURLConnectionimport java.net.URL
fun saveSceneToBlob( license: String, userId: String, uploadUrl: String,) = CoroutineScope( Dispatchers.Main,).launch { val engine = Engine.getInstance(id = "ly.img.engine.example") engine.start(license = license, userId = userId) engine.bindOffscreen(width = 100, height = 100)
val sceneUri = Uri.parse( "https://cdn.img.ly/assets/demo/v1/ly.img.template/templates/cesdk_postcard_1.scene", ) val scene = engine.scene.load(sceneUri = sceneUri)
val savedSceneString = engine.scene.saveToString(scene = scene)
val blob = savedSceneString.toByteArray(Charsets.UTF_8)
runCatching { withContext(Dispatchers.IO) { val connection = URL(uploadUrl).openConnection() as HttpURLConnection connection.requestMethod = "POST" connection.doOutput = true connection.outputStream.use { it.write(blob) } connection.connect() } }
engine.stop()}
Save Scenes to a String
In this example, we will show you how to save scenes as a string with the CreativeEditor SDK.
This is done by converting the contents of a scene to a single string, which can then be stored or transferred.
To get hold of such a string, you need to use engine.scene.saveToString()
.
This is an asynchronous method.
After waiting for the coroutine to finish, we receive a plain string holding the entire scene currently loaded in the editor.
This includes all pages and any hidden elements, but none of the actual asset data.
val savedSceneString = engine.scene.saveToString(scene = scene)
The returned string consists solely of ASCII characters and can safely be used further or written to a database.
println(savedSceneString)
Full Code
import android.net.Uriimport kotlinx.coroutines.CoroutineScopeimport kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.launchimport ly.img.engine.Engine
fun saveSceneToString( license: String, userId: String,) = CoroutineScope(Dispatchers.Main).launch { val engine = Engine.getInstance(id = "ly.img.engine.example") engine.start(license = license, userId = userId) engine.bindOffscreen(width = 100, height = 100)
val sceneUri = Uri.parse( "https://cdn.img.ly/assets/demo/v1/ly.img.template/templates/cesdk_postcard_1.scene", ) val scene = engine.scene.load(sceneUri = sceneUri)
val savedSceneString = engine.scene.saveToString(scene = scene)
println(savedSceneString)
engine.stop()}