Skip to main content
Language:

From Remote URL

Download the photo file#

Although you can pass a URL directly to the editor, we strongly recommend that you manage to download the remote resource yourself. This allows you more control over where and how the download task is being executed and avoids potential problems around UI locking.

Here, we download a photo from a remote URL using coroutines and save it in a local file. While the download is running, we show a loader and dismiss it when the download is finished.

Start Editor#

Configure the LoadSettings to set the source to the Uri of the file. 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 OpenPhotoFromRemoteURL(private val activity: AppCompatActivity) : Example(activity) {
// Although the editor supports adding assets with remote URLs, we highly recommend
// that you manage the download of remote resources yourself, since this
// gives you more control over the whole process.
override fun invoke() {
activity.lifecycleScope.launch {
showLoader(true)
val file = withContext(Dispatchers.IO) {
runCatching {
val file = File.createTempFile("imgly_photo", ".jpg")
URL("https://img.ly/static/example-assets/LA.jpg").openStream().use { input ->
file.outputStream().use { output ->
input.copyTo(output)
}
}
file
}.getOrNull()
}
showLoader(false)
file?.let {
showEditor(Uri.fromFile(it))
} ?: showMessage("Error downloading the file")
}
}
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 -> showMessage("Result saved at ${result.resultUri}")
else -> {
}
}
}
}
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()
}
}