class OpenPhotoFromCamera(private val activity: AppCompatActivity) : Example(activity) {
companion object {
private const val CAMERA_REQUEST_CODE = 0x69
}
private val file = File(activity.cacheDir, "imgly_photo.jpg")
override fun invoke() {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
try {
val photoURI: Uri = FileProvider.getUriForFile(activity, "ly.img.fileprovider", file)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
activity.startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE)
} catch (ex: ActivityNotFoundException) {
showMessage("No Camera app installed")
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
super.onActivityResult(requestCode, resultCode, intent)
when (requestCode) {
EDITOR_REQUEST_CODE -> {
intent ?: return
val result = EditorSDKResult(intent)
when (result.resultStatus) {
EditorSDKResult.Status.CANCELED -> showMessage("Editor cancelled")
EditorSDKResult.Status.EXPORT_DONE -> showMessage("Result saved at ${result.resultUri}")
else -> {
}
}
}
CAMERA_REQUEST_CODE -> {
if (resultCode == Activity.RESULT_OK) {
showEditor(Uri.fromFile(file))
}
}
}
}
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()
}
}