Skip to main content
Language:

To Base64

Start Editor#

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

By default, PhotoEditor SDK exports the photo as a temporary file in the application's cache directory.

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. The Uri of the exported photo can be accessed via EditorSDKResult.resultUri. Here, we launch a coroutine to read the bytes of the exported photo and then encode them into a Base64 string while showing a loader.

File:
class SavePhotoToBase64(private val activity: AppCompatActivity) : Example(activity) {
override fun invoke() {
// In this example, we do not need access to the Uri(s) after the editor is closed
// so we pass false 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)
}
// 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 -> {
activity.lifecycleScope.launch {
showLoader(true)
val base64String = withContext(Dispatchers.IO) {
runCatching {
val bytes = activity.contentResolver.openInputStream(result.resultUri!!)?.readBytes()
Base64.encodeToString(bytes, Base64.DEFAULT)
}.getOrNull()
}
showLoader(false)
base64String?.let {
showMessage("Received Base64 encoded string with ${it.length} characters")
} ?: showMessage("Error encoding to Base64")
}
}
else -> {
}
}
}
}
}