ArcObjects Java IMemoryBlobStream

3953
4
02-25-2015 04:22 AM
OwainCatton
New Contributor III

Hi All,

I have written a Java addin tool to open all attachments on a point feature in the PCs default image viewer.

I was using the Working with Attachments, ArcObjects Help for .NET developers , as a starting example to achieve this.

But when I use the code snippet below, it successfully loops through all the attachments reading the name

ITableAttachments attachTable;
attachTable = new ITableAttachmentsProxy(pFeatureClass);
IAttachmentManager attachMan = attachTable.getAttachmentManager();
ILongArray IdArray = new LongArray();
IdArray.add(OID);
IEnumAttachment attachEnum = attachMan.getAttachmentsByParentIDs(IdArray, true);
attachEnum.reset();
IAttachment attach = attachEnum.next();
while (attach != null){
 System.out.printf("%s,: %s\n", attach.getAttachmentID(), attach.getName() );
 IMemoryBlobStream memBlob = attach.getData();
 memBlob.saveToFile(attach.getName());
 attach = attachEnum.next();
}

But it fails to save the IMemoryBlobStream to file with the error,

java.lang.NullPointerException

which implies the blob data was not retrieved from the attachment table.

Any pointer on where I am going wrong would be much appreciated,

Owain

0 Kudos
4 Replies
ErinBrimhall
Occasional Contributor II

Hi Owain,

It looks like the parameter value provided to the saveToFile method needs to be a folder path, not the name of the file being saved.  I haven't worked with these interfaces before, but looking at this snippet I think it might be the change you need to make.

0 Kudos
OwainCatton
New Contributor III

Hi Erin,

That unfortunately using a folder or full path string does not work either

As far as I can tell the IAttachment.getData() is not retrieving the blob data into the IMemoryBlobStream hence the NULL pointer.

As any attempt to get information on the IMemoryBlobStream fails as its is NULL.

I will try and use the snippet mentioned to see if that works,

Owian

0 Kudos
OwainCatton
New Contributor III

For those that are interested, I have managed to get this working.

Although I ended up getting the Blob data directly from the table and not relying on the IAttachmentManager to retrieve it for me.

ITableAttachments attachTable;
attachTable = new ITableAttachmentsProxy(pFeatureClass);
IAttachmentManager attachMan = attachTable.getAttachmentManager();
ILongArray IdArray = new LongArray();
IdArray.add(OID);
IEnumAttachment attachEnum = attachMan.getAttachmentsByParentIDs(IdArray, true);
attachEnum.reset();
IAttachment attach = attachEnum.next();
ArrayList<String> imgList = new ArrayList<String>();
while (attach != null){
 String aPath = System.getenv("TEMP")+attach.getName();
 IDataset pDataset = (IDataset) pFeatureLayer;
 String tablePath = pDataset.getWorkspace().getPathName();
 IWorkspaceFactory pWorkspaceFact = new FileGDBWorkspaceFactory();
 IWorkspace pWorkspace = pWorkspaceFact.openFromFile(tablePath, 0);
 IFeatureWorkspace pFtWorkspace = (IFeatureWorkspace) pWorkspace;
 ITable pTable = pFtWorkspace.openTable(pDataset.getBrowseName()+"__ATTACH");
 IRow pRow = pTable.getRow(attach.getAttachmentID());
 IMemoryBlobStream memoryBlobStream = (IMemoryBlobStream) pRow.getValue(5);
 memoryBlobStream.saveToFile(aPath);
}

Hope this is some help to other people,

Owain

0 Kudos
ErinBrimhall
Occasional Contributor II

Nice, thanks for posting this solution.

0 Kudos