Select to view content in your preferred language

add attachments to a feature layer

380
1
08-01-2023 06:43 AM
majdkassab
Emerging Contributor

how to get ArcGis feature from servicefeature table to perform add attachments.

I tried this example and built on it with a different feature layer. 
https://github.com/Esri/arcgis-maps-sdk-kotlin-samples/tree/main/edit-feature-attachments

I logged few messages such as the image size, name and if the adding operation is finished but when I check the uploaded attachments there is non. I check the docs as well but didn't find any example or a written code that helped. it lacks in that department 

this is what I attempted, check the screen shot the 3rd one after trying to upload a image

private fun addFeatureAttachment(selectedImageUri: Uri) {
// display a loading dialog
val dialog = createLoadingDialog("Adding feature attachment").also {
it.show()
}

// create an input stream at the selected URI
contentResolver.openInputStream(selectedImageUri)?.use { imageInputStream ->
// get the byte array of the image input stream
val imageBytes: ByteArray = imageInputStream.readBytes()
// create the attachment name with the current time
val attachmentName = "attachment_${System.currentTimeMillis()}.png"
Log.d("attachment","attachment_${System.currentTimeMillis()}.png")

lifecycleScope.launch {
selectedArcGISFeature?.let { arcGISFeature ->
// add the attachment to the selected feature
arcGISFeature.addAttachment(
name = attachmentName,
contentType = "image/png",
data = imageBytes
).onFailure {
return@launch showError(it.message.toString())
}
// update the feature changes in the loaded service feature table
serviceFeatureTable.updateFeature(arcGISFeature).getOrElse {
return@launch showError(it.message.toString())
}
}
applyServerEdits(dialog)
}
}
}
private suspend fun applyServerEdits(dialog: AlertDialog) {
// close the bottom sheet, as it will be created
// after service changes are made
bottomSheet?.dismiss()

// apply edits to the server
val updatedServerResult = serviceFeatureTable.applyEdits()
updatedServerResult.onSuccess { edits ->
dialog.dismiss()

// check that the feature table was successfully updated
if (edits.isEmpty()) {
showToast("not")

return showError(getString(R.string.failure_edit_results))

}
// if the edits were made successfully, create the bottom sheet to display new changes.
selectedArcGISFeature?.let { createBottomSheet(it) }
}.onFailure {
showError(it.message.toString())
dialog.dismiss()
}
}

fun selectAttachment() {
val mediaIntent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
activityResultLauncher.launch(mediaIntent)
}



Thank you 

0 Kudos
1 Reply
majdkassab
Emerging Contributor

Hello, @MikeWilburn
Can you please provide any solutions 

Tags (1)
0 Kudos