class OpenPhotoFromGallery(private val activity: AppCompatActivity) : Example(activity) {
companion object {
private const val GALLERY_REQUEST_CODE = 0x69
}
override fun invoke() {
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
try {
activity.startActivityForResult(intent, GALLERY_REQUEST_CODE)
} catch (ex: ActivityNotFoundException) {
showMessage("No Gallery app installed")
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
super.onActivityResult(requestCode, resultCode, intent)
intent ?: return
when (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 -> {
}
}
}
GALLERY_REQUEST_CODE -> {
if (resultCode == Activity.RESULT_OK) {
intent.data?.let { showEditor(it) } ?: showMessage("Invalid Uri")
} else if (resultCode == Activity.RESULT_CANCELED) {
showMessage("User cancelled selection")
}
}
}
}
private fun showEditor(uri: Uri) {
val settingsList = PhotoEditorSettingsList(false)
.configure<LoadSettings> {
it.source = uri
}
PhotoEditorBuilder(activity)
.setSettingsList(settingsList)
.startActivityForResult(activity, EDITOR_REQUEST_CODE)
settingsList.release()
}
}