Skip to main content
PESDK/Android/Guides/Open / Load Photo
Language:

From Gallery

Open the Gallery for result using the ACTION_PICK action and MediaStore.Images.Media.EXTERNAL_CONTENT_URI Uri.

If the gallery exits with Activity.RESULT_OK, we get the Uri of the selected photo using Intent.getData().

Start Editor#

Configure the LoadSettings to set the source to the Uri of the selected photo. The PhotoEditorBuilder takes in the configured PhotoEditorSettingsList and starts the photo editor. The result is obtained in the onActivityResult() method when the editor exits.

File:
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) {
// 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 = uri
}
// 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()
}
}