Add attachment to a feature using Xamarin Android

1211
4
Jump to solution
08-02-2017 02:47 PM
YifanLiu
New Contributor II

Hi,

I am adding an attachment to a feature on the feature service. My issue is I am able to add the attachment to the feature, but I am not sure how to push the attachment to the feature service. Here is what I did:

private async void save() {
    await feature.LoadAsync();
    // bytes is the byte array containing data of the file
    var result = await feature.AddAttachmentAsync(App._file.Name, @"image/", bytes);
    
    ServiceFeatureTable sft = feature.FeatureTable as ServiceFeatureTable;
    await sft.UpdateFeatureAsync(feature);
    await sft.ApplyEditsAsync();
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Debugging shows that feature.AddAttachmentAsync() works as expected. After that line, I tried feature.GetAttachmentAsync() and it returns the just added attachment. However, I was not able to sync the attachment to the feature service. That feature service has attachment enabled.

Am I missing any steps here? Appreciate any help.

Best

Yifan

0 Kudos
1 Solution

Accepted Solutions
JenniferNery
Esri Regular Contributor

You can check for `CanEditAttachments` to know if you're able to add/update/delete attachments for the specified feature. You can also check the result of `ApplyEditsAsync` to know whether any of the edit operation failed.

if (feature.CanEditAttachments)
    await feature.AddAttachmentAsync("test.png", "image/png", bytes);
var table = layer.FeatureTable as ServiceFeatureTable;
var edits = await table.ApplyEditsAsync();
var editResult = edits.FirstOrDefault(r => r is FeatureEditResult && ((FeatureEditResult)r).AttachmentResults.FirstOrDefault(ar => ar.CompletedWithErrors || ar.Error != null) != null) as FeatureEditResult;

‍‍‍‍‍‍

In your code, is `App._file.Name` full file name including path? This should be file name with extension. And the `contentType` parameter should be one of the MIME types that your server support. (i.e. image/png, text/plain, etc.)

View solution in original post

4 Replies
NagmaYasmin
Occasional Contributor III

Hi Yifan,

Have you tried to set up GenerateGeodatabaseParameters.AttachmentSyncDirection to sets the direction for the attachments to be synchronized.

GenerateGeodatabaseParameters.AttachmentSyncDirection Property 

Hope that helps.

Nagma

0 Kudos
YifanLiu
New Contributor II

Thanks for the reply Nagma. I am developing for Android and the API is different from WPF. So, unfortunately, your suggestion would not work. Still appreciate it!

JenniferNery
Esri Regular Contributor

You can check for `CanEditAttachments` to know if you're able to add/update/delete attachments for the specified feature. You can also check the result of `ApplyEditsAsync` to know whether any of the edit operation failed.

if (feature.CanEditAttachments)
    await feature.AddAttachmentAsync("test.png", "image/png", bytes);
var table = layer.FeatureTable as ServiceFeatureTable;
var edits = await table.ApplyEditsAsync();
var editResult = edits.FirstOrDefault(r => r is FeatureEditResult && ((FeatureEditResult)r).AttachmentResults.FirstOrDefault(ar => ar.CompletedWithErrors || ar.Error != null) != null) as FeatureEditResult;

‍‍‍‍‍‍

In your code, is `App._file.Name` full file name including path? This should be file name with extension. And the `contentType` parameter should be one of the MIME types that your server support. (i.e. image/png, text/plain, etc.)

YifanLiu
New Contributor II

Hi Jennifer,

What I was missing is in this line:

 await feature.AddAttachmentAsync("test.png", "image/png", bytes);

I put "image/" instead of "image/png", which is what breaks things.

Thank you!

Best,

Yifan

0 Kudos